java UI皮肤


import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;


public class UiManager extends JFrame implements ActionListener{
	private JPanel rbPanel,itemPanel;
	private LookAndFeel laf;
	private JComboBox jcb;
	private String[] item = {"美女", "帅哥"};
	private JButton one, two, three;
	private ButtonGroup buttons;
	private JRadioButton[] rButton;
	private String[] bName = {"metal", "nimbus", "motif", "window"};
	
    public UiManager() {
    	super("UiManager");
		
		Container c = getContentPane();
		c.setBackground(Color.WHITE);
		setLayout(new FlowLayout(FlowLayout.CENTER, 50, 50));
    	
    	//单选按钮面板初始化
    	rButton = new JRadioButton[4];
    	buttons = new ButtonGroup();
    	rbPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 0));
    	for (int i = 0; i < 2; i++) {
    		rButton[i] = new JRadioButton(bName[i], i==0?true:false);
    		rButton[i].addActionListener(this);
    		buttons.add(rButton[i]);
    		rbPanel.add(rButton[i]);
	}
		
		//调试组件面板初始化
		jcb = new JComboBox(item);
		one = new JButton("one");
		two = new JButton("two");
		three = new JButton("three");
		itemPanel = new JPanel(new GridLayout(4,1,0,10));
		itemPanel.add(jcb);
		itemPanel.add(one);
		itemPanel.add(two);
		itemPanel.add(three);
		
		rbPanel.setBackground(Color.WHITE);
		itemPanel.setBackground(Color.WHITE);
		
		c.add(rbPanel);
		c.add(itemPanel);
    	
    	setSize(400, 400);
    	setLocationRelativeTo(null);
    	setDefaultCloseOperation(3);
    	setVisible(true);
    }
    
    public void actionPerformed(ActionEvent e){
    	JRadioButton selectedButton = (JRadioButton)e.getSource();
    	int selectedIndex = 0;
    	
    	for (int i = 0; i < 2; i++) {
    		if(rButton[i].isSelected()){
    			selectedIndex = i;
    			break;
    		}
		}
		
		selectedLAF(selectedIndex);
    }
    
    //根据单选按钮的选择情况  来选择UI
    public void selectedLAF(int selectedIndex){
    	switch(selectedIndex){
    		case 0:
    			laf = new MetalLookAndFeel();
    			break;
    		case 1:
    			laf = new NimbusLookAndFeel();
    			break;

    	}
    	
    	try {
    		UIManager.setLookAndFeel(laf);
    		
    		//这个方法是刷新整个UI  必须添加
    		SwingUtilities.updateComponentTreeUI(this);
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
    }
    
    public static void main(String[] args) {
    	new UiManager();
    }
}


你可能感兴趣的:(java UI皮肤)