import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class CheckBox extends JFrame implements ItemListener
{
JCheckBox ch1,ch2;
JPanel p1,p2;
Container conn;
CheckBox()
{
conn=getContentPane();
conn.setBackground(Color.WHITE);
p1=new JPanel();p1.setBackground(Color.yellow);
p2=new JPanel();p2.setBackground(Color.red);
ch1=new JCheckBox("Blue",false);ch1.setOpaque(false);
ch2=new JCheckBox("Red",false);ch2.setOpaque(false);
conn.add(ch1);conn.add(ch2);
ch1.addItemListener(this);ch2.addItemListener(this);
}
public void itemStateChanged(ItemEvent e )
{
if (ch1.isSelected()==true)
{
conn.add(p1);p1.setBounds(30,40,50,30);
this.repaint();
}
if(ch1.isSelected()==false)
{
conn.remove(p1);p1.setBounds(30,40,50,30);
this.repaint();
}
if (ch2.isSelected()==true)
{
conn.add(p2);p2.setBounds(100,40,50,30);
this.repaint();
}
if (ch2.isSelected()==false)
{
conn.remove(p2);p2.setBounds(100,40,50,30);
this.repaint();
}
}
public static void main(String[] args)
{
JFrame win = new CheckBox();
win.setTitle("CheckBox");
win.setSize(200,120);
win.setLayout(new FlowLayout());
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setVisible(true);
}
}
Output
No comments:
Post a Comment