如何在JAVA高级GUI中实现换肤功能?

来源:百度知道 编辑:UC知道 时间:2024/05/20 15:08:42
如题

要实现换肤功能,需要调用类UIManager的静态方法getinstalledlookandfeels,使用系统提供的默认的lookandfeel,然后加入GUI,同时要加监听,实现点击选项换肤的功能。比如说要把皮肤选项作为JMenuItem,具体部分代码如下:
UIManager.LookAndFeelInfo[] uil=new UIManager().getInstalledLookandFeels;
for(int i=0;i<uil.length;i++){
JMenuItem jmi=new JMenuItem(uil[i].getname()+"外观");
jmi.addActionListener(new UIActionListener());//加监听器
jmi.setActionCommand(uil[i].getClassName());//加按钮动作
}
class UIActionListener implements ActionListener(){

//重写actionPerformed方法
public void actionPerformed(ActionEvent e) {

String name = e.getActionCommand();

try {
UIManager.setLookAndFeel(name);

SwingUtilities.updateComponentTreeUI(GUI面板);

}
catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (InstantiationException e1) {

e1.printStackTrace()