在 70 岁时,我决定学习 Java,我必须说这项任务有时给我带来了很多挫败感。为了显示可供选择的项目列表,我决定使用 JOptionFrame
类的 showInputDialog
选项,我发现每当通过调用 UIManager 将错误字体更改为等宽字体时,该选项都可以完美对齐,如下所示: UIManager.put("List.font", new Font( "monospaced",Font.PLAIN,14));
问题是,每当我在我认为所谓的父框架中插入列表时,字体就会返回到默认值,这将不再起作用。我可以毫无问题地插入其他对象,但不能插入列表。我的问题是:我做错了什么?相关代码如下。
UIManager.put("List.font", new Font( "monospaced",Font.PLAIN,14));
String input = (String) JOptionPane.showInputDialog(null,
"World Exchanges",
"Please select one", JOptionPane.QUESTION_MESSAGE, null,
choices, choices[0]); // Initial choice
最佳答案
我怀疑这是外观和感觉缓存行为不一致的结果。 UIManager
的 javadoc 是这样说的:
The set of defaults a particular look and feel supports is defined and documented by that look and feel. In addition, each look and feel, or ComponentUI provided by a look and feel, may access the defaults at different times in their life cycle. Some look and feels may aggressively look up defaults, so that changing a default may not have an effect after installing the look and feel. Other look and feels may lazily access defaults so that a change to the defaults may effect an existing look and feel. Finally, other look and feels might not configure themselves from the defaults table in any way. None-the-less it is usually the case that a look and feel expects certain defaults, so that in general a ComponentUI provided by one look and feel will not work with another look and feel.
我的建议(基于此)是调用此电话:
UIManager.put("List.font", new Font("monospaced", Font.PLAIN,14));
选择外观之后(如果您这样做),在实例化任何 Swing 组件之前。
关于java - 更改 JOptionPane.showInputDialog 中默认字体的过程给出了混合结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43443801/