设置Swing程序的默认字体

Con*_*lle 59 java fonts swing uimanager

我想知道如何为我的整个Java swing程序设置默认字体.根据我的研究,它似乎可以完成,与UIManager某些事情有关LookAndFeel,但我找不到具体的方法,UIManager看起来很复杂.

Rom*_*eau 55

尝试:

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);
      }
    } 
Run Code Online (Sandbox Code Playgroud)

打电话给......

setUIFont (new javax.swing.plaf.FontUIResource("Serif",Font.ITALIC,12));
Run Code Online (Sandbox Code Playgroud)

  • 出于某种原因,这只是改变JTabbedPane上选项卡文本的字体. (2认同)
  • 我让它使用`UIManager.getLookAndFeelDefaults()`而不是`UIManager.getDefaults()`并使用返回的引用而不是`UIManager.put()`(见下面我的答案)。它仅在覆盖默认 L&F 时才有效(在我的情况下为 Nimbus)... (2认同)

Ami*_*far 37

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 */);
Run Code Online (Sandbox Code Playgroud)

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

  • 源码来源:[BasicLookAndFeel.java](http://hg.openjdk.java.net/jdk7/jsn/jdk/file/tip/src/share/classes/javax/swing/plaf/basic/BasicLookAndFeel.java) (2认同)
  • 附加信息:某些组件有多个键来设置组件不同部分的字体。例如 JOptionPane:要设置 JOptionPane 的默认字体,您应该使用: `UIManager.put("OptionPane.messageFont", /*font*/);` 和 `UIManager.put("OptionPane.buttonFont", /*font* /);` (2认同)

Bom*_*mbe 18

java -Dswing.aatext=true -Dswing.plaf.metal.controlFont=Tahoma -Dswing.plaf.metal.userFont=Tahoma …
Run Code Online (Sandbox Code Playgroud)

这不仅可以在您的完整UI上设置Tahoma,还可以启用抗锯齿功能,使任何字体立即变得更加美观.

  • 效果很好.并设置一个字体,其名称中有空格,您可以引用.还可以通过添加" - "指定字体大小:-Dswing.plaf.metal.controlFont ="Droid Sans-15"(大小15) (2认同)

she*_*rif 8

我认为这更好,称之为当前的laf,而不是整个UIManager把它

UIManager.getLookAndFeelDefaults()
        .put("defaultFont", new Font("Arial", Font.BOLD, 14));
Run Code Online (Sandbox Code Playgroud)

在实例化JFrame对象之前的主要位置.它对我来说很完美.请记住,对于没有指定字体的组件,这是默认字体.

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


Pav*_*dek 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 更准确的方法是“UIManager.put(key, new javax.swing.plaf.FontUIResource(fr.deriveFont(fr.getSize2D() * 2.0f)));” (2认同)

Jea*_*ana 5

请注意,设置默认字体的方式取决于您使用的外观。Romain Hippeau 描述的解决方案适用于很多 LAF,但不适用于 Nimbus。sherif 发布的那篇文章在 Nimbus 上工作得很好,但对其他人(例如 Metal)则不行。

将两者结合可以适用于大多数 LAF,但这些解决方案都不适用于 GTK+ LAF。

我认为(但我不确定),没有跨平台的解决方案。