JAva UI肌

2595 ワード

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();
    }
}