您的位置:首页 > 产品设计 > UI/UE

Swing 改变观感 UIManager.setLookAndFeel UIManager.update.updateLookAndFeel(..)

2009-02-27 09:42 281 查看
改变观感:
方法一:
在jre/lib下的swing.properties中,将swing.defaultlaf设为:
Swing.defaultlaf = com.sun.java.swing.plaf.motif.MotifLookAndFeel
注:
在属性文件中,以#开头的行被省略,所以可以保留原来的观感,便于修改。这种方式必须重启程序。

方法二:
动态的改变观感。
注:
嵌套类中This发生混淆时,要使用ClassName.this来指明。
关键是如下两条:
UIManager.setLookAndFeel(lookName);
根据观感的名字设定其观感。
SwingUtilities.updateComponentTreeUI(LookAndFeel.this);
给定一个组件后,更新所有的组件。

例子:
/**
*LookAndFeel.java
* Created on 9:01:54 AM Feb 27, 2009
*@author Quasar063501
*@version 0.1
*
*/
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class LookAndFeel extends JFrame {
private JPanel buttonPanel = null;

public void launchFrame() {
this.setSize(200,300);
buttonPanel = new JPanel();
//的到所有安装的观感
UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
//new for
for(UIManager.LookAndFeelInfo info : infos) {
makeButton(info.getName(),info.getClassName());
}

this.add(buttonPanel);
this.setVisible(true);
}

private void makeButton(String bName, final String lookName) {
JButton b = new JButton(bName);
buttonPanel.add(b);

b.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {
try {
UIManager.setLookAndFeel(lookName);
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (InstantiationException e1) {
e1.printStackTrace();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
} catch (UnsupportedLookAndFeelException e1) {
e1.printStackTrace();
}
SwingUtilities.updateComponentTreeUI(LookAndFeel.this);
}

});
}

public static void main(String[] args) {
new LookAndFeel().launchFrame();
}
}
相关类:
javax.swing.UIManager
javax.seing.UIManager.LookAndFeelInfo
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: