import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class ColorchooserTest extends JFrame implements ActionListener{
    JButton button;
    
    public ColorchooserTest(){
        button = new Jbutton("Click Me");
        button.addActionListener(this);
        
        JPanel pane = new JPanel();
        pane.setLayout(new GridLayout(1,1));
        pane.add(button);
        
        Container cpane = getContentPane();
        cpane.setLayout(new GridLayout(1,1));
        cpane.add(pane);
        
        setTitle("TEST");
        setSize(320, 240);
        setVisible(true);
    }
    
    
    public void actionPerformed(ActionEvent arg0) {
        if(arg0.getSource() == button){
            Color c = null;
            c =JColorchooser.showDialog(button,"a Color", button.getBackground());
            System.out.print(c);
            button.setBackground(c);
        }
    }

    
    public static void main(String[] args) {
        new ColorchooserTest();
    }
}

沒有留言:

張貼留言