face1.gif
face2.gif
import javax.swing.*;import java.awt.*;
import java.awt.event.*;
class jrbtest extends JFrame implements ItemListener
{
ButtonGroup bg;
JRadioButton jrb1,jrb2,jrb3;
ImageIcon ii1,ii2;
JTextField jtf1;
JPanel pane;
public jrbtest()
{ super("JRadioButton Testing");
setSize(100,250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ii1=new ImageIcon("face1.gif");
ii2=new ImageIcon("face2.gif");
bg=new ButtonGroup();
jrb1=new JRadioButton("day",ii1,false);
jrb2=new JRadioButton("month",ii1,false);
jrb3=new JRadioButton("year",ii2,true);
jtf1=new JTextField("year",10);
bg.add(jrb1);
bg.add(jrb2);
bg.add(jrb3);
pane=new JPanel();
pane.setLayout(new GridLayout(4,1));
pane.add(jrb1);
pane.add(jrb2);
pane.add(jrb3);
pane.add(jtf1);
setContentPane(pane);
setVisible(true);
jrb1.addItemListener(this);
jrb2.addItemListener(this);
jrb3.addItemListener(this);
}
public void itemStateChanged(ItemEvent e)
{ if(jrb1.isSelected())
{ jrb1.setIcon(ii2);
jrb2.setIcon(ii1);
jrb3.setIcon(ii1);
jtf1.setText("day");
}
if(jrb2.isSelected())
{ jrb1.setIcon(ii1);
jrb2.setIcon(ii2);
jrb3.setIcon(ii1);
jtf1.setText("month");
}
if(jrb3.isSelected())
{ jrb1.setIcon(ii1);
jrb2.setIcon(ii1);
jrb3.setIcon(ii2);
jtf1.setText("year");
}
}
public static void main(String args[])
{ new jrbtest();
}
}
No comments:
Post a Comment