/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package cn.tsp2c.gui.event;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
/**
*
* @author Administrator
*/
public class PlafTest{
public static void main(String[] args){
PlatFrame pf = new PlatFrame();
pf.setVisible(true);
}
}
class PlatFrame extends JFrame{
public PlatFrame(){
setTitle("切换主题");
setSize(400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//创建面板对象,燃耗加入到窗体中
PlafPanel plafPanel = new PlafPanel();
add(plafPanel);
}
}
//构建一个面板来放置按钮对象
class PlafPanel extends JPanel{
public PlafPanel(){
UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
for(UIManager.LookAndFeelInfo info : infos){
//用每一种安装的主题生成一个对应的按钮,然后点击每一个按钮要切换到相应的主题
String title = info.getName();
String palfClazz = info.getClassName();
makeButton(title,palfClazz);
}
}
private void makeButton(String title,final String plafClazz){
JButton btn = new JButton(title);
add(btn);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
//当按钮按下时,切换到相应的主题
UIManager.setLookAndFeel(plafClazz);
//刷新面板中的所有组件
SwingUtilities.updateComponentTreeUI(PlafPanel.this);
}catch (Exception ex){
Logger.getLogger(PlafPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
}