2012-03-31 15 views
2

即時嘗試更改整個應用程序的全局字體,問題是,我只能做到這一次。這裏是幫助你重現問題的代碼。全局更改整個框架的字體JAVA

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package test; 

import java.awt.Font; 
import java.util.Enumeration; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 
import javax.swing.UIManager; 

/** 
* 
* @author osiris 
*/ 
public class Test { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) throws InterruptedException { 
     // TODO code application logic here 
     JFrame frame = new JFrame(); 
     frame.setSize(600, 600); 
     frame.setVisible(true); 
     JButton button = new JButton("test"); 
     frame.add(button); 
     frame.repaint(); 
     Thread.sleep(2000); 
     Font font = new Font("Berlin Sans FB",java.awt.Font.PLAIN,14); 
     Enumeration test = UIManager.getDefaults().keys(); 

     while (test.hasMoreElements()) { 

      Object key = test.nextElement(); 
      Object value = UIManager.get(key); 
      if (value instanceof Font) { 
       UIManager.put(key, font); 
      } 
     } 
     SwingUtilities.updateComponentTreeUI(frame); 

     Thread.sleep(2000); 
     Font font2 = new Font("Tempus Sans ITC",java.awt.Font.PLAIN,14); 

     test = UIManager.getDefaults().keys(); 

     while (test.hasMoreElements()) { 

      Object key = test.nextElement(); 
      Object value = UIManager.get(key); 
      if (value instanceof Font) { 
       UIManager.put(key, font2); 
      } 
     } 
     SwingUtilities.updateComponentTreeUI(frame); 
    } 
} 

按鈕上的字體只改變一次,爲什麼?爲什麼第二次沒有改變?

回答

4

任何/通過設置/更改所有更改已經顯現Container /修改值(S)爲UIManager/UIDeafaultsLook and Feel rellated問題,比你要打電話

SwingUtilities.updateComponentTreeUI(frame); 

編輯,如果你想更新字體在運行時,那麼你必須更改FontUIResource不是簡單的字體

enter image description hereenter image description hereenter image description here

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.plaf.FontUIResource; 
import javax.swing.plaf.basic.BasicComboBoxRenderer; 

public class SystemFontDisplayer extends JFrame { 

    private static final long serialVersionUID = 1L; 
    private JFrame frame = new JFrame("Nimbus UIDeafaults and Font"); 
    private JComboBox fontsBox; 
    private javax.swing.Timer timer = null; 
    private JButton testButton = new JButton("testButton"); 
    private JTextField testTextField = new JTextField("testTextField"); 
    private JLabel testLabel = new JLabel("testLabel"); 

    public SystemFontDisplayer() { 
     GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
     String[] fontFamilyNames = ge.getAvailableFontFamilyNames(); 
     fontsBox = new JComboBox(fontFamilyNames); 
     fontsBox.setSelectedItem(0); 
     fontsBox.setRenderer(new ComboRenderer(fontsBox)); 
     fontsBox.addItemListener(new ItemListener() { 

      @Override 
      public void itemStateChanged(ItemEvent e) { 
       if (e.getStateChange() == ItemEvent.SELECTED) { 
        final String fontName = fontsBox.getSelectedItem().toString(); 
        fontsBox.setFont(new Font(fontName, Font.PLAIN, 16)); 
        start(); 
       } 
      } 
     }); 
     fontsBox.setSelectedItem(0); 
     fontsBox.getEditor().selectAll(); 
     frame.setLayout(new GridLayout(4, 0, 20, 20)); 
     frame.add(fontsBox); 
     frame.add(testButton); 
     frame.add(testTextField); 
     frame.add(testLabel); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLocation(200, 105); 
     frame.pack(); 
     java.awt.EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       fontsBox.setPopupVisible(true); 
       fontsBox.setPopupVisible(false); 
      } 
     }); 
     frame.setVisible(true); 
    } 

    private void start() { 
     timer = new javax.swing.Timer(750, updateCol()); 
     timer.setRepeats(false); 
     timer.start(); 
    } 

    public Action updateCol() { 
     return new AbstractAction("text load action") { 

      private static final long serialVersionUID = 1L; 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       final Font fnt = new Font(fontsBox.getSelectedItem().toString(), Font.PLAIN, 12); 
       final FontUIResource res = new FontUIResource(fnt); 
       UIManager.getLookAndFeelDefaults().put("Button.font", res); 
       UIManager.getLookAndFeelDefaults().put("TextField.font", res); 
       UIManager.getLookAndFeelDefaults().put("Label.font", res); 
       SwingUtilities.updateComponentTreeUI(frame); 
      } 
     }; 
    } 

    public static void main(String arg[]) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       SystemFontDisplayer systemFontDisplayer = new SystemFontDisplayer(); 
      } 
     }); 
    } 

    private class ComboRenderer extends BasicComboBoxRenderer { 

     private static final long serialVersionUID = 1L; 
     private JComboBox comboBox; 
     final DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer(); 
     private int row; 

     private ComboRenderer(JComboBox fontsBox) { 
      comboBox = fontsBox; 
     } 

     private void manItemInCombo() { 
      if (comboBox.getItemCount() > 0) { 
       final Object comp = comboBox.getUI().getAccessibleChild(comboBox, 0); 
       if ((comp instanceof JPopupMenu)) { 
        final JList list = new JList(comboBox.getModel()); 
        final JPopupMenu popup = (JPopupMenu) comp; 
        final JScrollPane scrollPane = (JScrollPane) popup.getComponent(0); 
        final JViewport viewport = scrollPane.getViewport(); 
        final Rectangle rect = popup.getVisibleRect(); 
        final Point pt = viewport.getViewPosition(); 
        row = list.locationToIndex(pt); 
       } 
      } 
     } 

     @Override 
     public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 
      super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 
      if (list.getModel().getSize() > 0) { 
       manItemInCombo(); 
      } 
      final JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, row, isSelected, cellHasFocus); 
      final Object fntObj = value; 
      final String fontFamilyName = (String) fntObj; 
      setFont(new Font(fontFamilyName, Font.PLAIN, 16)); 
      return this; 
     } 
    } 
} 
+0

謝謝你,這當然有幫助,但我仍然有一些問題。我已將字體更改爲BOLD,並且您看到菜單上的字體爲BOLD,但菜單項仍然很簡單,請查看截圖http://i39.tinypic.com/vfi939.png。任何想法爲什麼是這樣的? – 2012-03-31 11:05:01

+0

@Povedz Heslo編輯你的文章與[SSCCE](http://sscce.org/),另一個問題可能是靜態類的初始化,也許不相關,也許是的 – mKorbel 2012-03-31 11:52:39

+0

我編輯我的第一篇文章,不知道我應該更多add there – 2012-03-31 12:41:49

1

默認值是在創建新UI組件時應用的值,並且未指定特定的覆蓋。

如果要更改默認值,則必須在創建第一幀之前執行此操作。

要創建UI後更改整個應用程序的字體,您需要遍歷所有現有組件並更改它們的字體字段(如果字體具有不同的大小,則需要重新繪製並可能重新佈局)。