JColorChooser

import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.colorchooser.*;

public class JColorChooserDemo2 implements ChangeListener{
	JFrame jf = null;
	JLabel label = null;
	JColorChooser colorChooser = null;
	
	public JColorChooserDemo2(){
		jf = new JFrame("JColorChooserDmoe2");
		Container contentPane = jf.getContentPane();
		
		label = new JLabel("",JLabel.CENTER);
		label.setOpaque(true);
		label.setForeground(Color.black);
		
		JPanel panel = new JPanel();
		colorChooser = new JColorChooser();
		AbstractColorChooserPanel[] colorPanels = colorChooser.getChooserPanels();
		colorChooser.removeChooserPanel(colorPanels[0]);
		panel.add(colorChooser);
		ColorSelectionModel selectionModel = colorChooser.getSelectionModel();
		selectionModel.addChangeListener(this);
		
		contentPane.add(label,BorderLayout.NORTH);
		contentPane.add(panel,BorderLayout.CENTER);
		
		jf.pack();
		jf.setVisible(true);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	@Override
	public void stateChanged(ChangeEvent ce){
		Color color = colorChooser.getColor();
		label.setBackground(color);
		label.setText("你選擇的顏色為,R:"+color.getRed()+" G:"+color.getGreen()+" B:"+color.getBlue());
	}
	
	public static void main(String...args){
		new JColorChooserDemo2();
	}
}

//Swatches HSB RGB 這3個面板都是已經構造好的面板,若要產生自己所設計的面板,可以繼承AbstractColorChooserPanel這個抽象類,并實現里面的抽象方法
//就可以用JColorChooser提供的addColorPanel()或setColorPanels()將自己設計的面板加入到顏色顯示面板中
//若要刪除面板,可以先使用JColorChooser提供的getChooserPanels()得到AbstractColorChooserPanel的
//Array Object.然後調用JColorChooser的removeChooserPanel()刪除面板

參考:深入淺出Java Swing程序設計

你可能感兴趣的:(java,swing)