设置Swing程序的默认字体

73

我想知道如何为整个Java Swing程序设置默认字体。根据我的研究,似乎可以使用UIManager完成,与LookAndFeel有关,但我找不到具体的方法,并且UIManager看起来非常复杂。


如果您看一下窗口右侧,然后向下滚动,您会看到“相关”栏,其中有3-5个关于类似问题的优秀主题。 - mKorbel
12个回答

72

尝试:

public static void setUIFont (javax.swing.plaf.FontUIResource f){
    java.util.Enumeration keys = UIManager.getDefaults().keys();
    while (keys.hasMoreElements()) {
      Object key = keys.nextElement();
      Object value = UIManager.get (key);
      if (value instanceof javax.swing.plaf.FontUIResource)
        UIManager.put (key, f);
      }
    } 

Call by ...

setUIFont (new javax.swing.plaf.FontUIResource("Serif",Font.ITALIC,12));

3
由于某些原因,这只会改变我的JTabbedPane选项卡上文本的字体。 - Connor Neville
2
我已经让它工作了,使用UIManager.getLookAndFeelDefaults()代替UIManager.getDefaults(),并使用返回的引用而不是UIManager.put()(请参见下面的答案)。然而,只有在覆盖默认的L&F时才有效(在我的情况下是Nimbus)。 - Matthieu

45
UIManager.put("Button.font", /* font of your liking */);
UIManager.put("ToggleButton.font", /* font of your liking */);
UIManager.put("RadioButton.font", /* font of your liking */);
UIManager.put("CheckBox.font", /* font of your liking */);
UIManager.put("ColorChooser.font", /* font of your liking */);
UIManager.put("ComboBox.font", /* font of your liking */);
UIManager.put("Label.font", /* font of your liking */);
UIManager.put("List.font", /* font of your liking */);
UIManager.put("MenuBar.font", /* font of your liking */);
UIManager.put("MenuItem.font", /* font of your liking */);
UIManager.put("RadioButtonMenuItem.font", /* font of your liking */);
UIManager.put("CheckBoxMenuItem.font", /* font of your liking */);
UIManager.put("Menu.font", /* font of your liking */);
UIManager.put("PopupMenu.font", /* font of your liking */);
UIManager.put("OptionPane.font", /* font of your liking */);
UIManager.put("Panel.font", /* font of your liking */);
UIManager.put("ProgressBar.font", /* font of your liking */);
UIManager.put("ScrollPane.font", /* font of your liking */);
UIManager.put("Viewport.font", /* font of your liking */);
UIManager.put("TabbedPane.font", /* font of your liking */);
UIManager.put("Table.font", /* font of your liking */);
UIManager.put("TableHeader.font", /* font of your liking */);
UIManager.put("TextField.font", /* font of your liking */);
UIManager.put("PasswordField.font", /* font of your liking */);
UIManager.put("TextArea.font", /* font of your liking */);
UIManager.put("TextPane.font", /* font of your liking */);
UIManager.put("EditorPane.font", /* font of your liking */);
UIManager.put("TitledBorder.font", /* font of your liking */);
UIManager.put("ToolBar.font", /* font of your liking */);
UIManager.put("ToolTip.font", /* font of your liking */);
UIManager.put("Tree.font", /* font of your liking */);

来源:http://www.jguru.com/faq/view.jsp?EID=340519


哇...通过向 /字体喜欢的/ 部分添加 New Font 对象是否能起作用?因为,一旦 JFRame 创建并运行...我没有得到任何效果。还有我忘了什么吗 @Amir Raminfar? - gumuruh
2
源代码来源:BasicLookAndFeel.java - flup
2
附加信息:某些组件具有多个键来设置组件不同部分的字体。例如JOptionPane:要设置JOptionPane的默认字体,您应该使用:UIManager.put("OptionPane.messageFont", /*font*/);UIManager.put("OptionPane.buttonFont", /*font*/); - matthiasboesinger
1
@gumuruh 当你刷新内容树 SwingUtilities.updateComponentTreeUI(this); 时它可以工作。 - Asce4s

23
java -Dswing.aatext=true -Dswing.plaf.metal.controlFont=Tahoma -Dswing.plaf.metal.userFont=Tahoma …

这不仅会将Tahoma字体应用于您的整个UI,还会打开抗锯齿功能,使任何字体立即更加漂亮。


4
非常好用。如果要设置带空格的字体名称,可以使用引号。同时,通过添加“-”来指定字体大小:-Dswing.plaf.metal.controlFont="Droid Sans-15" (大小为15)。 - Emmanuel Touzery
完全不起作用。抛出未识别选项错误。 - Brian Knoblauch

14

我认为这样会更好,调用当前laf而不是整个UIManager,把它放在这里

UIManager.getLookAndFeelDefaults()
        .put("defaultFont", new Font("Arial", Font.BOLD, 14));

在实例化JFrame对象之前,在主程序的某个位置。 这对我来说完美地工作了。 请记住,这是默认字体,适用于没有指定字体的组件。

来源: http://www.java.net/node/680725


使用调色板、拖放功能制作的 NetBeans Swing 应用程序可以正常工作。 - Thufir

7
受Romain Hippeau的启发,如果您只想设置字体大小,请使用此代码。
for (Map.Entry<Object, Object> entry : javax.swing.UIManager.getDefaults().entrySet()) {
    Object key = entry.getKey();
    Object value = javax.swing.UIManager.get(key);
    if (value != null && value instanceof javax.swing.plaf.FontUIResource) {
        javax.swing.plaf.FontUIResource fr=(javax.swing.plaf.FontUIResource)value;
        javax.swing.plaf.FontUIResource f = new javax.swing.plaf.FontUIResource(fr.getFamily(), fr.getStyle(), FONT_SIZE);
        javax.swing.UIManager.put(key, f);
    }
}

2
一种更准确的方法是 UIManager.put(key, new javax.swing.plaf.FontUIResource(fr.deriveFont(fr.getSize2D() * 2.0f))); - Cheok Yan Cheng

5
请注意,设置默认字体的方式取决于您所使用的外观和感觉。Romain Hippeau描述的解决方案在许多外观中都有效,但在Nimbus中无效。sherif发布的解决方案在Nimbus上有效,但在其他外观(例如Metal)上无效。将两种解决方案结合起来可以在大多数外观上使用,但这些解决方案都不能在GTK+界面外观上使用。我认为(但不确定),没有跨平台的解决方案。

4
作为@Amir回答的补充,这是完整的键列表。
我使用这个函数。
private void setFont(FontUIResource myFont) {
    UIManager.put("CheckBoxMenuItem.acceleratorFont", myFont);
    UIManager.put("Button.font", myFont);
    UIManager.put("ToggleButton.font", myFont);
    UIManager.put("RadioButton.font", myFont);
    UIManager.put("CheckBox.font", myFont);
    UIManager.put("ColorChooser.font", myFont);
    UIManager.put("ComboBox.font", myFont);
    UIManager.put("Label.font", myFont);
    UIManager.put("List.font", myFont);
    UIManager.put("MenuBar.font", myFont);
    UIManager.put("Menu.acceleratorFont", myFont);
    UIManager.put("RadioButtonMenuItem.acceleratorFont", myFont);
    UIManager.put("MenuItem.acceleratorFont", myFont);
    UIManager.put("MenuItem.font", myFont);
    UIManager.put("RadioButtonMenuItem.font", myFont);
    UIManager.put("CheckBoxMenuItem.font", myFont);
    UIManager.put("OptionPane.buttonFont", myFont);
    UIManager.put("OptionPane.messageFont", myFont);
    UIManager.put("Menu.font", myFont);
    UIManager.put("PopupMenu.font", myFont);
    UIManager.put("OptionPane.font", myFont);
    UIManager.put("Panel.font", myFont);
    UIManager.put("ProgressBar.font", myFont);
    UIManager.put("ScrollPane.font", myFont);
    UIManager.put("Viewport.font", myFont);
    UIManager.put("TabbedPane.font", myFont);
    UIManager.put("Slider.font", myFont);
    UIManager.put("Table.font", myFont);
    UIManager.put("TableHeader.font", myFont);
    UIManager.put("TextField.font", myFont);
    UIManager.put("Spinner.font", myFont);
    UIManager.put("PasswordField.font", myFont);
    UIManager.put("TextArea.font", myFont);
    UIManager.put("TextPane.font", myFont);
    UIManager.put("EditorPane.font", myFont);
    UIManager.put("TabbedPane.smallFont", myFont);
    UIManager.put("TitledBorder.font", myFont);
    UIManager.put("ToolBar.font", myFont);
    UIManager.put("ToolTip.font", myFont);
    UIManager.put("Tree.font", myFont);
    UIManager.put("FormattedTextField.font", myFont);
    UIManager.put("IconButton.font", myFont);
    UIManager.put("InternalFrame.optionDialogTitleFont", myFont);
    UIManager.put("InternalFrame.paletteTitleFont", myFont);
    UIManager.put("InternalFrame.titleFont", myFont);
}

在调用用户界面之前,我会在main中调用它。

setFont(new FontUIResource(new Font("Cabin", Font.PLAIN, 14)));

要查看完整的Swing UI Manager键列表,请查看此链接


1
注意在调用UI之前调用它是必要的,否则只有少数地方会出现字体效果。 - yolob 21

1
正确答案是由Amir Raminfar给出的,但您需要将字体封装为FontUIResource。
例如:
UIManager.put("Button.font", new FontUIResource(new Font ("Helvetica", Font.BOLD, 16)));

1
抱歉,我认为这段代码更好: UIManager.put("Button.font", new FontUIResource("Helvetica", Font.BOLD, 16)); - Nicolas GOUDARD

0
为了解决这个问题,我只需实现AWTEventListener并监听ContainerEvent的COMPONENT_ADDED事件。
所有故事描述请参见:http://wiki.idempiere.org/en/Swing_Miss_Support_Some_Language 所有代码请参见:https://bitbucket.org/hieplq/unicentapos/src/9b22875ab65e26ff46fd9ae62d556b7f64621afa/src-extend/vn/hsv/uitil/font/FontGlyphsUtil.java?at=tip
  1. 实现AWTEventListener

 

public void eventDispatched(AWTEvent event) {
    if (!isMissSupportGlyph || !(event instanceof ComponentEvent) || !(event instanceof ContainerEvent))
        return;

    if (event instanceof ContainerEvent){
        ContainerEvent containerEvent = (ContainerEvent)event;
        if (containerEvent.getID() == ContainerEvent.COMPONENT_ADDED){
            updateChildControlFont(containerEvent.getChild());
        }
    }
}
  1. 添加注册表监听器(最好在程序启动时运行)

 

Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.COMPONENT_EVENT_MASK | AWTEvent.CONTAINER_EVENT_MASK);

0

我正在使用Nimbus L&F。

使用@Romain Hippeau的代码,我不得不使用UIManager.getLookAndFeelDefaults()而不是UIManager.getDefaults(),并使用返回的引用来put修改后的值:

    int szIncr = 5; // Value to increase the size by
    UIDefaults uidef = UIManager.getLookAndFeelDefaults();
    for (Entry<Object,Object> e : uidef.entrySet()) {
        Object val = e.getValue();
        if (val != null && val instanceof FontUIResource) {
            FontUIResource fui = (FontUIResource)val;
            uidef.put(e.getKey(), new FontUIResource(fui.getName(), fui.getStyle(), fui.getSize()+szIncr));
        }
    }

出于某种原因,默认的 L&F 似乎无法正常工作...(基于我进行的有限测试)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接