Thursday, January 30, 2014

Example of JClorChooser in java.

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

class mahi1 implements ActionListener
{
JFrame f;
JButton b;
JPanel p;
mahi1(String s)
{
f=new JFrame(s);

b=new JButton("Color");
b.setBackground(Color.yellow);
b.addActionListener(this);
f.add(b,BorderLayout.NORTH);

p = new JPanel();
p.setLayout(new FlowLayout());
f.add(p);

f.setResizable(false);
f.setSize(300,200);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
Color bg = JColorChooser.showDialog(f,"Choose background color", Color.white);
       
if(bg != null)
{
               p.setBackground(bg);
               f.getContentPane().setBackground(bg);
        }
}
public static void main(String surat[])
{
new mahi1("MaHi1");
}
}

Output:

No comments:

Post a Comment