2015-05-27 27 views
2

我如何可以更改顯示在我的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或JComboBoxes

沒有工作,然後我試圖通過遍歷所有鍵設置字體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); 
     } 
    } 
} 

這段代碼根本不起作用。

我必須說,我用咸陽爲LookAndFeel的...所以我不知道怎麼說通過UIManager的我manuell設置干擾。

+1

[此外,它使您能夠修改現有的主題和創建自己的樣子,只有通過編輯基於XML的配置文件感覺 - 你不必編寫複雜的Java的GUI碼(http://www.javasoft.de/synthetica/) – mKorbel

+0

。投票tgo關閉主題 – mKorbel

回答

0

我想你可以從Roman更新this answer如下:

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); 
     } 
} 

那麼跟你Font像這樣使用它:

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

*「'新的java.awt.Font(」Arial Unicode MS「..」「*爲了更大的跨平臺支持,使用邏輯字體(例如'Font.SANS_SERIF',而不是''Arial Unicode MS「')。 –

+0

同意......但我只是在原來的問題中使用了'字體'.... –

+0

它沒有工作。我調試了代碼,併爲每個組件編寫了值,但UI仍然相同。 Synthetica是否可能忽略這些manuell設置? – mareicha

0

您可以創建自己的組件,如:

public class MyJButton extends JButton { 

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

} 

public class MyJTextArea extends JTextArea { 

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

} 
. 
. 
. 

然後創建這個類,然後H裝所有的字體屬性,如:

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); 
    } 

} 

我想這是原始的方式,但如果你想保存在你的數據庫 這些值也可以是很好的