Swing 字体名称不匹配? (制作字体选择器,并尝试在 JComboBox 中显示默认系统字体)
我正在创建一个 Swing 字体选择器。 (另请参阅:如何防止 JComboBox使用自定义 ListCellRenderer 时变得无响应)
为了获取所有可用字体,我这样做:
GraphicsEnvironment gE = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] fonts = gE.getAllFonts();
在包含所有可用字体的字体选择器(JComboBox)中,我想初始化它,显示所选的默认系统字体。
为此,我使用静态基本字体(从静态基本标签(JLabel)获取),从中获得默认字体。 然后,在 JComboBox 初始化期间,我调用
fontComboBox.setSelectedItem(new Font(baseFont.getName(),baseFont.getStyle(),1));
//The size is 1 in all fonts retrieved from GraphicsEnvironment.
//The combo box contains objects of the type Font.
将所选字体设置为默认系统字体。
这在大多数情况下都有效。尽管如此,对于某些字体来说,创建的基本字体与从 GraphicsEnvironment 检索的任何字体都不匹配。
对我来说,基本字体是 Dialog。字体的名称是“Dialog”。但是,从 GraphicsEnvironment for Dialog 检索的字体名称为“Dialog.Bold”、“Dialog.Plain”和“Dialog.Italic”。由于创建的字体具有不同的名称,组合框将不会选择所需的项目(equals(...) 失败)。
请注意这对于大多数字体是如何工作的(到目前为止我测试过的唯一一种不起作用的是对话框字体)。
有没有更好的解决方案可以避免这个问题?我是否可以通过使用 JLabel 以外的某些特定的其他 Swing 组件来获取基本字体来获取正确的字体名称? 最后,为什么名字不匹配?
编辑:后续问题:如何加载逻辑字体物理字体? (制作JComboBox字体选择器)
I am creating a swing font chooser. (See also: How to prevent JComboBox from becoming unresponsive when using a custom ListCellRenderer)
To get all available fonts, I do:
GraphicsEnvironment gE = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] fonts = gE.getAllFonts();
In my font chooser (a JComboBox), which contains all the available fonts, I want to initialize it showing the default system font as selected.
To do this, I use a static base font (which is gotten from a static base label (a JLabel)), from which I get the default font.
Then, during initialization of the JComboBox, I call
fontComboBox.setSelectedItem(new Font(baseFont.getName(),baseFont.getStyle(),1));
//The size is 1 in all fonts retrieved from GraphicsEnvironment.
//The combo box contains objects of the type Font.
to set the selected font to the default system font.
This works most of the time. Although, it appears that for some fonts, the created base font does not match any of the fonts which are retrieved from GraphicsEnvironment.
For me, the base font is Dialog. The name of the font is "Dialog". However, the names for the fonts retrieved from GraphicsEnvironment for Dialog are "Dialog.Bold", "Dialog.Plain", and "Dialog.Italic". Since the created font has a different name, the combobox will not select the desired item (equals(...) fails).
Note how this works with most fonts (the only one I have tested so far that doesn't work is the Dialog font).
Is there a better solution to this that avoids this problem? Can I perhaps get the correct font names by using some specific other Swing component than JLabel to get the base font?
Finally, why don't the names match?
EDIT: Followup question: How does one load a logical fonts physical font? (Making a JComboBox font chooser)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一些字体“Dialog”、“Monospaced”,还有一些不记得名称的字体不是 rel 字体。 Java 使用人造字体,但它们是用另一种物理字体呈现的。这样做是为了提供独立于平台的字体名称。
这意味着例如对于“等宽”字体,使用了一些具有相同字符宽度的依赖于操作系统的真实字体。
http://download.oracle.com/javase/1.3/docs /guide/intl/addingfonts.html
There are some fonts "Dialog", "Monospaced" and some more don't remember names aren't rel fonts. The artificial fonts are used by java but they are rendered with another physical font. It's done to provide platform independent font names.
It means e.g. for "Monospaced" font some OS dependent real font with equal chars' widhts is used.
http://download.oracle.com/javase/1.3/docs/guide/intl/addingfonts.html