在 Swing 中自定义标签字体

发布于 2024-07-29 08:57:05 字数 471 浏览 2 评论 0原文

在我的 swing 应用程序中,我想要显示不同类型的文本。 例如,我想在选项列表之前显示标题文本,例如:

    Select choice:
     a
     b

我希望“Select Choice”标签使用“Heading”字体(我定义的东西),并且选项使用“Choice”字体。 这种模式将出现在我的应用程序中的多个位置,因此理想情况下我希望集中设置字体的位置。

我当前的方法是使用工厂来创建不同的标签类型:

LabelFactory.createHeadingLabel("LabelText");
LabelFactory.createChoiceLabel("ChoiceText");

工厂读取指定字体的属性文件,并在工厂中创建标签时自定义标签。 我知道使用这样的工厂是可行的,但我不确定是否有一个标准的 Swing 约定来执行这样的操作。 任何意见将不胜感激。

In my swing application, I have different types of text I'd like to display. For example, I want to display a heading text before a list of choices, something like:

    Select choice:
     a
     b

I want the "Select Choice" label to use the "Heading" font (something I define), and the choices to use the "Choice" font. This pattern will occur multiple places in my application, so ideally I'd like to centralize where the fonts are set.

My current approach is to use a factory to create the different label types:

LabelFactory.createHeadingLabel("LabelText");
LabelFactory.createChoiceLabel("ChoiceText");

The factory reads in a properties file specifying the fonts and I customize the labels when they are created in the factory. I know that using a factory like this works, but I'm not sure if there is a standard Swing convention for doing something like this. Any input would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恍梦境° 2024-08-05 08:57:05

另一种方法是扩展 JLabel。

您仍然可以从配置文件中读取字体,但这样您就可以以与常规 JLabel 更相似的方式创建标签。

private JLabel header = new HeaderJLabel("Header text");

您所要做的就是覆盖自定义标签的构造函数。

另外,您应该只读取配置文件一次。 将其加载到内存中,然后从内存中访问它。

One other approach would be to extend JLabel.

You could still read the fonts in from a configuration file, but this way, you can create labels in a much more similar way to a regular JLabel.

private JLabel header = new HeaderJLabel("Header text");

All you would have to do is override the constructor for your custom labels.

Also, you should only read in the configuration file once. Load it into memory, and access it from memory from then on.

单身狗的梦 2024-08-05 08:57:05

您可以在许多 Swing 组件内使用有限的 html,因此您可以使用 html 为文本提供不同的样式。

例如,headingLabel.setText("< html>此文本将以粗体显示< /html>");
会给你一个带有粗体文本的标签。

注意:您必须删除 << 中的多余空格。 html> 且< /html>

You can use limited html inside of many Swing components, so you may be able to use html to give your text different styles.

For instance, headingLabel.setText("< html><b>This text will be bold</b>< /html>");
Will give you a label with bold text.

Note: you have to remove the extra space from < html> and < /html>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文