import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.color.*;
public class SampleFrame2
{
public static void main(String args[])
{
MyFrame F=new MyFrame();
}
}
class MyFrame extends JFrame implements ActionListener
{
JPanel p=new JPanel();
JCheckBox a=new JCheckBox ("YELLOW");
JCheckBox b=new JCheckBox("BLUE");
JCheckBox c=new JCheckBox("RED");
JCheckBox d=new JCheckBox("GREEN");
MyFrame()
{
p.add(a);
p.add(b);
p.add(c);
p.add(d);
a.addActionListener(this);
b.addActionListener(this);
c.addActionListener(this);
d.addActionListener(this);
add(p);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==a)
{
p.setBackground(Color.yellow);
}
if(e.getSource()==b)
{
p.setBackground(Color.blue);
}
if(e.getSource()==c)
{
p.setBackground(Color.red);
}
if(e.getSource()==d)
{
p.setBackground(Color.green);
}
}
}
output:
No comments:
Post a Comment