日期:2014-05-20  浏览次数:20685 次

SWING做的界面,怎样一次性改变所有同一类型的组件的属性?
用SWING做的界面,里面有很多的同一类型的组件,比如五个同样大小的按钮,怎样用一句语句改变这五个按钮上的字号的大小?我说的是用一句语句统一改变,感觉好像要一个一个BUTTON的重设字号很麻烦!这五个按钮在同一个布局管理器中,能用一条语句改吗?
如果不在同一个PANEL或其它容器中呢?能用一条语句改吗?
------解决方案--------------------
把这种Button定义成一个类,设置字体的那一行代码放在它的构造方法内,可以不
------解决方案--------------------
Swing技巧. 设置全局字体(sun jdk)

为什么要这么做?
   因为java默认的字体显示中文都很难看
   因为比如jgoodies这样的skin默认不支持中文
   因为jdk1.4中文字体mapping有严重bug,用过IDEA的人都知道
   因为大家只有sun的jdk可用,ibm的,bea的都不适合跑client

if you are smart....
//设置全局字体
public static void initGlobalFontSetting(Font fnt){
    FontUIResource fontRes = new FontUIResource(fnt);
    for(Enumeration keys = UIManager.getDefaults().keys(); keys.hasMoreElements();){
        Object key = keys.nextElement();
        Object value = UIManager.get(key);
        if(value instanceof FontUIResource)
            UIManager.put(key, fontRes);
    }
}
if you aren't...

Font font = new Font("Dialog",Font.PLAIN,12);
UIManager.put("ToolTip.font",font);
UIManager.put("Table.font",font);
UIManager.put("TableHeader.font",font); 
UIManager.put("TextField.font",font); 
UIManager.put("ComboBox.font",font); 
UIManager.put("TextField.font",font); 
UIManager.put("PasswordField.font",font); 
UIManager.put("TextArea.font",font); 
UIManager.put("TextPane.font",font); 
UIManager.put("EditorPane.font",font); 
UIManager.put("FormattedTextField.font",font); 
UIManager.put("Button.font",font); 
UIManager.put("CheckBox.font",font); 
UIManager.put("RadioButton.font",font); 
UIManager.put("ToggleButton.font",font); 
UIManager.put("ProgressBar.font",font); 
UIManager.put("DesktopIcon.font",font); 
UIManager.put("TitledBorder.font",font); 
UIManager.put("Label.font",font); 
UIManager.put("List.font",font); 
UIManager.put("TabbedPane.font",font); 
UIManager.put("MenuBar.font",font); 
UIManager.put("Menu.font",font); 
UIManager.put("MenuItem.font",font); 
UIManager.put("PopupMenu.font",font); 
UIManager.put("CheckBoxMenuItem.font",font); 
UIManager.put("RadioButtonMenuItem.font",font); 
UIManager.put("Spinner.font",font); 
UIManager.put("Tree.font",font); 
UIManager.put("ToolBar.font",font); 
UIManager.put("OptionPane.messageFont",font); 
UIManager.put("OptionPane.buttonFont",font); 

From: http://blog.sina.com.cn/s/blog_703e3c800100mzmn.html
------解决方案--------------------
可以考虑用UIManager;