Java 如何更改所有组件的字体

Java 如何更改所有组件的字体,java,swing,fonts,uimanager,Java,Swing,Fonts,Uimanager,如何更改java应用程序中显示的所有组件的字体 我用UIManager试过了 UIManager.put("TextField.font", new java.awt.Font("Arial Unicode MS", java.awt.Font.PLAIN, 11)); UIManager.put("Label.font", new java.awt.Font("Arial Unicode MS", java.awt.Font.PLAIN, 11)); UIManager.put("Comb

如何更改java应用程序中显示的所有组件的字体

我用UIManager试过了

UIManager.put("TextField.font",  new java.awt.Font("Arial Unicode MS", java.awt.Font.PLAIN, 11));
UIManager.put("Label.font",  new java.awt.Font("Arial Unicode MS", java.awt.Font.PLAIN, 11));
UIManager.put("ComboBox.font",  new java.awt.Font("Arial Unicode MS", java.awt.Font.PLAIN, 11))
奇怪的是,这改变了文本字段的字体,但对jlabel或jcombobox不起作用

然后我尝试通过循环UIManager知道的所有键来设置字体:

public static void setUIFont(FontUIResource f) {
    Enumeration keys = UIManager.getDefaults().keys();
    while (keys.hasMoreElements()) {
        Object key = keys.nextElement();
        Object value = UIManager.get(key);
        if (value instanceof FontUIResource) {
            FontUIResource orig = (FontUIResource) value;
            Font font = new Font(f.getFontName(), orig.getStyle(), f.getSize());
            FontUIResource fontUIResource = new FontUIResource(font);
            UIManager.put(key, fontUIResource);
            UIManager.getDefaults().put(key, fontUIResource);
            UIManager.getLookAndFeel().getDefaults().put(key, fontUIResource);
        }
    }
}
这个代码根本不起作用

我必须说,我使用合成纤维作为外观和感觉。。。因此,我不知道这会如何干扰我通过UIManager进行的manuell设置。

我认为您可以通过以下方式进行更新:

public static void setUIFont (java.awt.Font f){
    java.util.Enumeration keys = UIManager.getDefaults().keys();
    while (keys.hasMoreElements()) {
      Object key = keys.nextElement();
      Object value = UIManager.get (key);
      if (value != null && value instanceof java.awt.Font)
        UIManager.put (key, f);
      }
}
然后将其与您的
字体一起使用,如下所示:

setUIFont (new java.awt.Font("Arial Unicode MS", java.awt.Font.PLAIN, 11));

您可以创建自己的组件,如:

public class MyJButton extends JButton {

    public MyJButton(){
        setFont(FontClass.getBasicFont());
    }

}

public class MyJTextArea extends JTextArea {

    public MyJTextArea (){
        setFont(FontClass.getBasicFont());
    }

}
.
.
.
然后创建包含所有字体属性的类,如:

public class FontClass(){

    private static final String BASIC_FONT_FACE = "Arial";

    private static final String BASIC_FONT_SIZE = 12;

    public static Font getBasicFont(){
        return new Font(BASIC_FONT_FACE, Font.PLAIN, BASIC_FONT_SIZE);
    }

}
我想这是最原始的方法,但如果你想省钱的话也可以
数据库中的这些值“
新java.awt.Font(“Arial Unicode MS”。
”要获得更大的跨平台支持,请使用逻辑字体(例如
Font.SANS\u SERIF
,而不是
“Arial Unicode MS”
)。同意……但我在原始问题中使用了
Font
。。它不起作用。我调试了代码,并为每个组件编写了值,但UI仍然是一样的。Synthetica是否会忽略这些manuell设置?这是一个复制粘贴的答案,您只是稍微更改了名称和数字。@user1803551实际上我在我的一个应用程序中使用它,是的,我想我是从那里开始使用它的,因为我的upvote仍然存在…问题在哪里?投票tgo作为关闭主题