import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Radiobutton extends JFrame implements ItemListener
{
JRadioButton first,second,third,fourth;
JPanel p1,p2,p3,p4;
Container conn;
Radiobutton()
{
conn=getContentPane();
conn.setBackground(Color.ORANGE);
p1=new JPanel();
p1.setBackground(Color.blue);
p2=new JPanel();
p2.setBackground(Color.red);
p3=new JPanel();
p3.setBackground(Color.black);
p4=new JPanel();
p4.setBackground(Color.yellow);
first = new JRadioButton("Blue");
second = new JRadioButton("red");
third = new JRadioButton("Black");
fourth = new JRadioButton("Yellow");
//Add on Frame
conn.add(first);conn.add(second);conn.add(third);conn.add(fourth);
ButtonGroup bg = new ButtonGroup();
bg.add(first);bg.add(second);bg.add(third);bg.add(fourth);
first.addItemListener(this);
second.addItemListener(this);
third.addItemListener(this);
fourth.addItemListener(this);
}
public void itemStateChanged(ItemEvent e )
{
if (first.isSelected())
{
conn.add(p1);
conn.remove(p2);conn.remove(p3);conn.remove(p4);
p1.setBounds(40,60,30,30);
this.repaint();
}
if (second.isSelected())
{
conn.add(p2);
conn.remove(p1);conn.remove(p3);conn.remove(p4);
p2.setBounds(100,60,30,30);
this.repaint();
}
if (third.isSelected())
{
conn.add(p3);
conn.remove(p2);conn.remove(p1);conn.remove(p4);
p3.setBounds(160,60,30,30);
this.repaint();
}
if (fourth.isSelected())
{
conn.add(p4);
conn.remove(p2);conn.remove(p3);conn.remove(p1);
p4.setBounds(220,60,30,30);
this.repaint();
}
}
public static void main(String[] args)
{
JFrame win = new Radiobutton();
win.setTitle("Radiobutton");
win.setSize(300,200);
win.setLayout(new FlowLayout());
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setVisible(true);
}
}Output
No comments:
Post a Comment