import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Rb extends JFrame implements ItemListener
{
JRadioButton image1;
JRadioButton image2;
JRadioButton image3;
JLabel img1;
JLabel img2;
JLabel img3;
ImageIcon I1=new ImageIcon("1.jpg");
ImageIcon I2=new ImageIcon("2.jpg");
ImageIcon I3=new ImageIcon("3.jpg");
ImageIcon none=new ImageIcon("");
JPanel checkpanel;
JPanel labelpanel;
public Rb(String title)
{
super(title);
image1=new JRadioButton("Image1");
image2=new JRadioButton("Image2");
image3=new JRadioButton("Image3");
img1=new JLabel();
img2=new JLabel();
img3=new JLabel();
checkpanel =new JPanel(new GridLayout(3,1));
checkpanel.add(image1);
checkpanel.add(image2);
checkpanel.add(image3);
labelpanel =new JPanel(new GridLayout(3,1));
labelpanel.add(img1);
labelpanel.add(img2);
labelpanel.add(img3);
image1.addItemListener(this);
image2.addItemListener(this);
image3.addItemListener(this);
this.setLayout(new GridLayout(1,2));
this.add(checkpanel);
this.add(labelpanel);
}
public void itemStateChanged(ItemEvent e)
{
if(e.getStateChange()==e.SELECTED)
{
if(e.getSource()==image1)
{
img1.setIcon(I1);
img1.setText("");}
}
else
if(e.getStateChange()==e.DESELECTED)
{
if(e.getSource()==image1)
{
img1.setIcon(none);
}
}
if(e.getStateChange()==e.SELECTED)
{
if(e.getSource()==image2)
{
img2.setIcon(I2);
img2.setText("");}
}
else
if(e.getStateChange()==e.DESELECTED)
{
if(e.getSource()==image2)
{
img2.setIcon(none);
}
}
if(e.getStateChange()==e.SELECTED)
{
if(e.getSource()==image3)
{
img3.setIcon(I3);
img3.setText("");}
}
else
if(e.getStateChange()==e.DESELECTED)
{
if(e.getSource()==image3)
{
img3.setIcon(none);
}
}}
public static void main(String args[])
{
Rb cb=new Rb("CheckBoxDemo");
cb.setVisible(true);
cb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cb.setSize(700,500);
}}
OUTPUT :
import javax.swing.*;
import java.awt.event.*;
public class Rb extends JFrame implements ItemListener
{
JRadioButton image1;
JRadioButton image2;
JRadioButton image3;
JLabel img1;
JLabel img2;
JLabel img3;
ImageIcon I1=new ImageIcon("1.jpg");
ImageIcon I2=new ImageIcon("2.jpg");
ImageIcon I3=new ImageIcon("3.jpg");
ImageIcon none=new ImageIcon("");
JPanel checkpanel;
JPanel labelpanel;
public Rb(String title)
{
super(title);
image1=new JRadioButton("Image1");
image2=new JRadioButton("Image2");
image3=new JRadioButton("Image3");
img1=new JLabel();
img2=new JLabel();
img3=new JLabel();
checkpanel =new JPanel(new GridLayout(3,1));
checkpanel.add(image1);
checkpanel.add(image2);
checkpanel.add(image3);
labelpanel =new JPanel(new GridLayout(3,1));
labelpanel.add(img1);
labelpanel.add(img2);
labelpanel.add(img3);
image1.addItemListener(this);
image2.addItemListener(this);
image3.addItemListener(this);
this.setLayout(new GridLayout(1,2));
this.add(checkpanel);
this.add(labelpanel);
}
public void itemStateChanged(ItemEvent e)
{
if(e.getStateChange()==e.SELECTED)
{
if(e.getSource()==image1)
{
img1.setIcon(I1);
img1.setText("");}
}
else
if(e.getStateChange()==e.DESELECTED)
{
if(e.getSource()==image1)
{
img1.setIcon(none);
}
}
if(e.getStateChange()==e.SELECTED)
{
if(e.getSource()==image2)
{
img2.setIcon(I2);
img2.setText("");}
}
else
if(e.getStateChange()==e.DESELECTED)
{
if(e.getSource()==image2)
{
img2.setIcon(none);
}
}
if(e.getStateChange()==e.SELECTED)
{
if(e.getSource()==image3)
{
img3.setIcon(I3);
img3.setText("");}
}
else
if(e.getStateChange()==e.DESELECTED)
{
if(e.getSource()==image3)
{
img3.setIcon(none);
}
}}
public static void main(String args[])
{
Rb cb=new Rb("CheckBoxDemo");
cb.setVisible(true);
cb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cb.setSize(700,500);
}}
OUTPUT :
No comments:
Post a Comment