/*
* 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);
}
}
});
}
}