Monday, December 30, 2013

JColorChooser sample Java

//JColorChooser Sample
// There will be a JFrame with a button in it. Click
// the button and choose a color the button color
// will be the chosen color
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class CColor extends JFrame implements ActionListener
{
    JButton jb1;
    JPanel pane;
    public CColor()
    {
        setSize(300,200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jb1=new JButton("Change Color");
        jb1.addActionListener(this);
        pane=new JPanel();
        pane.add(jb1);
        setContentPane(pane);
        setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==jb1)
        {
          Color bgColor= JColorChooser.showDialog(this,"Choose Color",getBackground());
          if (bgColor != null)
            jb1.setBackground(bgColor);
        }
    }
    public static void main(String args[])
    {
        new CColor();
    }
}

No comments:

Post a Comment