Java 设置Swing程序的默认字体

Java 设置Swing程序的默认字体,java,swing,fonts,uimanager,Java,Swing,Fonts,Uimanager,我想知道如何为我的整个JavaSwing程序设置默认字体。从我的研究来看,它似乎可以通过UIManager来完成,与LookAndFeel有关,但我找不到具体的方法,而且UIManager看起来相当复杂 UIManager.put("Button.font", /* font of your liking */); UIManager.put("ToggleButton.font", /* font of your liking */); UIManager.put("RadioButton.f

我想知道如何为我的整个JavaSwing程序设置默认字体。从我的研究来看,它似乎可以通过
UIManager
来完成,与
LookAndFeel
有关,但我找不到具体的方法,而且
UIManager
看起来相当复杂

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 */);
来源:

试试:

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);
      }
    } 
打电话给

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

这不仅会在您的整个UI上设置Tahoma,而且还会打开抗锯齿功能,使任何字体立即变得更加美观。

我认为这更好,将其称为当前laf而不是整个UIManager 把这个

在实例化JFrame对象之前,在main中的某个位置。 它对我非常有效。 请记住,对于没有指定字体的组件,这是默认字体


来源:

灵感来源于罗曼·希波,如果您只想设置字体大小,请使用此代码

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);
    }
}
for(Map.Entry:javax.swing.UIManager.getDefaults().entrySet()){
Object key=entry.getKey();
objectvalue=javax.swing.UIManager.get(key);
if(value!=null&&value instanceof javax.swing.plaf.FontUIResource){
javax.swing.plaf.FontUIResource fr=(javax.swing.plaf.FontUIResource)值;
javax.swing.plaf.FontUIResource f=新的javax.swing.plaf.FontUIResource(fr.getFamily(),fr.getStyle(),FONT\u SIZE);
javax.swing.UIManager.put(key,f);
}
}

请注意,设置默认字体的方式取决于您使用的外观。 罗曼·希波(Romain Hippeau)所描述的解决方案适用于大量LAF,但不适用于Nimbus。谢里夫发布的这篇文章对Nimbus很好,但对其他的(比如金属)就不行

将两者结合起来可以在大多数LAF上工作,但这些解决方案都不能与GTK+LAF一起工作

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

我使用的是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));
        }
    }
int szIncr=5;//值将大小增加
UIDefaults uidef=UIManager.getLookAndFeelDefaults();
对于(条目e:uidef.entrySet()){
对象val=e.getValue();
if(val!=null&&val FontUIResource实例){
FontUIResource fui=(FontUIResource)val;
uidef.put(e.getKey(),new-FontUIResource(fui.getName(),fui.getStyle(),fui.getSize()+szIncr));
}
}

由于某些原因,它似乎不适用于默认的L&F。。。(基于我执行的有限测试)

为了解决这个问题,我只需实现一个AWTEventListener并侦听ContainerEvent添加的组件

所有故事描述位于:

所有代码位于:

  • 实现AWTEventListener
  • 添加注册表侦听器(最好在启动程序时运行)

  • 这些解决方案都不适合我,我建立了自己的(愚蠢的)解决方案,但它确实有效:

    private void changeFontRecursive(Container root, Font font) {
        for (Component c : root.getComponents()) {
            c.setFont(font);
            if (c instanceof Container) {
                changeFontRecursive((Container) c, font);
            }
        }
    }
    

    正确答案是Amir Raminfar给出的答案,但您必须将字体封装为FontUIResource

    例如:

    UIManager.put("Button.font", new FontUIResource(new Font ("Helvetica", Font.BOLD, 16)));
    
    我使用了XML文件并在那里定义了字体。简单、灵活、大洲式。
    您需要使用
    createFont
    创建一个类,如:

    public class CustomFontResource {
    public static FontUIResource createFont(String path, final int size) throws IOException, FontFormatException {
        Font font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(path));
        return new FontUIResource(font.deriveFont(Font.PLAIN, size));
    }
    
    在synth xml中定义如下字体:

        <object id="Basic_Regular" class="<your CustomFontResource class>"
            method="createFont">
        <string>path_to_your_font</string>
        <int>font_size</int>
    </object>
    
    
    指向字体的路径
    字体大小
    

    然后,您可以在xml中的任何地方使用它作为参考。

    作为@Amir answer的补充,这是完整的键列表

    我使用这个函数

    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);
    }
    
    在调用ui之前,我在
    main
    中调用它

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

    要查看Swing UI管理器键的完整列表,请选中此

    如果您要查看此窗口上的正确站点,请向下滚动,我可以看到列
    相关
    ,其中有3-5个关于类似问题的优秀线程出于某种原因,这只是在更改我的JTabbedPane上选项卡文本的字体。我让它使用
    UIManager.getLookAndFeelDefaults()
    而不是
    UIManager.getDefaults()
    ,并使用返回的引用而不是
    UIManager.put()
    (请参阅下面的答案)。它只在覆盖默认L&F时有效(在我的例子中是Nimbus)…哇。。。将新字体对象添加到您喜欢的/*字体*/部分是否有效?Bcoz,看来一旦JFRame创建并运行。。。我没有得到任何效果。有什么我忘记了@Amir Raminfar吗?来源:附加信息:一些组件有多个键来设置组件不同部分的字体。例如JOptionPane:要设置JOptionPane的默认字体,您应该使用:
    UIManager.put(“OptionPane.messageFont”,/*font*/)
    UIManager.put(“OptionPane.buttonFont”,/*font*/)
    @gumuruh当您刷新内容树
    SwingUtilities.updateComponentTreeUI(此)时,它会工作非常有效。要设置名称中有空格的字体,可以引用。另外,通过添加“-”来指定字体大小:--Dswing.plaf.metal.controlFont=“Droid Sans-15”(大小15)对我来说根本不起作用。抛出无法识别的选项错误。适用于使用调色板、拖放制作的netbeans swing应用程序。抱歉,我认为这段代码更好:UIManager.put(“Button.font”,new-FontUIResource(“Helvetica”,font.BOLD,16));更准确的方法是
    UIManager.put(key,newjavax.swing.plaf.FontUIResource(fr.deriveFont(fr.getSize2D()*2.0f))注意在调用ui之前调用它是必要的,否则只有少数地方会发生字体效果。
    
    public class CustomFontResource {
    public static FontUIResource createFont(String path, final int size) throws IOException, FontFormatException {
        Font font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(path));
        return new FontUIResource(font.deriveFont(Font.PLAIN, size));
    }
    
        <object id="Basic_Regular" class="<your CustomFontResource class>"
            method="createFont">
        <string>path_to_your_font</string>
        <int>font_size</int>
    </object>
    
    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);
    }
    
    setFont(new FontUIResource(new Font("Cabin", Font.PLAIN, 14)));