import java.awt.*;
import java.awt.event.*;
public class CheckboxDemo extends Frame implements ItemListener,WindowListener {
CheckboxGroup cbg=new CheckboxGroup();
Checkbox c1=new Checkbox("RED",cbg,false);
Checkbox c2=new Checkbox("GREEN",cbg,false);
Checkbox c3=new Checkbox("BLUE",cbg,false);
Checkbox c4=new Checkbox("YELLOW",cbg,false);
Checkbox c5=new Checkbox("BLACK",cbg,false);
CheckboxDemo(){
super("CheckBox Selection");
setSize(400,500);
setLayout(new FlowLayout());
add(c1);
add(c2);
add(c3);
add(c4);
add(c5);
c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);
c4.addItemListener(this);
c5.addItemListener(this);
addWindowListener(this);
setVisible(true);
}
public void windowActivated(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowClosing(WindowEvent e){
System.exit(0);
}
public void windowDeactivated(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void itemStateChanged(ItemEvent e){
if(c1.getState()==true)
{
this.setBackground(Color.red);
}
else
if(c2.getState())
{
setBackground(Color.green);
}
else
if(c3.getState())
{
setBackground(Color.blue);
}
else
if(c4.getState())
{
setBackground(Color.yellow);
}
else
if(c5.getState())
{
setBackground(Color.black);
}
}
public static void main(String[] ar){
CheckboxDemo m=new CheckboxDemo();
}
}
No comments:
Post a Comment