java.lang.Object
--java.awt.Component
--java.awt.Container
--javax.swing.JComponent
--javax.swing.Abstractbutton
--javax.swing.JToggleButton
--javax.swing.JRadioButton
JRadioButtonBN J JToggleButton的一个了类,因此它也可以使用AbstractButton抽象类里的方法。如同前面所述,JChexkBox 主要用在多重选择时机,而JRadioButton则是运用在单一选择时机。
JRadioButton构造函数:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JRadioButton1 implements ItemListener
{
JFrame f = null;
JRadioButton r4 = null;
JRadioButton r5 = null;
JRadioButton1(){
f = new JFrame("JRadioButton");
Container contentPane = f.getContentPane();
contentPane.setLayout(new GridLayout(2,1));
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(1,3));
p1.setBorder(BorderFactory.createTitledBorder("您最喜欢哪一家速食店呢?"));
JRadioButton r1 = new JRadioButton("麦当劳");
JRadioButton r2 = new JRadioButton("肯德鸡");
JRadioButton r3 = new JRadioButton("21世纪");
p1.add(r1);
p1.add(r2);
p1.add(r3);
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(2,1));
p2.setBorder(BorderFactory.createTitledBorder("您喜欢哪种程序语言? 喜欢的请打勾"));
r4 = new JRadioButton("JAVA",new ImageIcon(".\\icons\\x.jpg"));
r5 = new JRadioButton("C++",new ImageIcon(".\\icons\\x.jpg"));
r4.addItemListener(this);
r5.addItemListener(this);
p2.add(r4);
p2.add(r5);
contentPane.add(p1);
contentPane.add(p2);
f.pack();
f.show();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void itemStateChanged(ItemEvent e)
{
if (e.getStateChange() == e.SELECTED)
{
if(e.getSource() == r4)
r4.setIcon(new ImageIcon(".\\icons\\r.jpg"));
if(e.getSource() == r5)
r5.setIcon(new ImageIcon(".\\icons\\r.jpg"));
}
else
{
if(e.getSource() == r4)
r4.setIcon(new ImageIcon(".\\icons\\x.jpg"));
if(e.getSource() == r5)
r5.setIcon(new ImageIcon(".\\icons\\x.jpg"));
}
}
public static void main(String args[])
{
new JRadioButton1();
}
}
要将RadioButton改成单选,我们必须用到ButtonGroup这个类。这个类位于javax.swing这个package下面,ButtonGroup类的主 要功能是:同一时间内只会有一个组件的状态为"on",其他皆为"off",也就是同一时间只有一个组件会被选取。而ButtonGroup类可 被AbstractButton下面的子类所使用,最常被使用的就是JRadioButton、JradioButtonMenu、Item与JToggleButton这些组件。
下面是ButtonGroup的类层次结构图:
java.lang.Object
--javax.swing.ButtonGroup
我们更改上例,使RadioButton变成单选吧!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JRadioButton2 implements ItemListener {
JFrame f = null;
JRadioButton r4 = null;
JRadioButton r5 = null;
JRadioButton2() {
f = new JFrame("JRadioButton2");
Container contentPane = f.getContentPane();
contentPane.setLayout(new GridLayout(2, 1));
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(1, 3));
p1.setBorder(BorderFactory.createTitledBorder("你最喜欢哪一家快餐店呢?"));
JRadioButton r1 = new JRadioButton("麦当劳");
JRadioButton r2 = new JRadioButton("肯德基");
JRadioButton r3 = new JRadioButton("21世纪");
p1.add(r1);
p1.add(r2);
p1.add(r3);
/*
* 将3个RadioButton对象放进ButtonGroup中,表示此3个RadioButton同一时间只有一个RadioButton的状态可以为
* "on";
*/
ButtonGroup bgroup1 = new ButtonGroup();
bgroup1.add(r1);
bgroup1.add(r2);
bgroup1.add(r3);
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(2, 1));
p2.setBorder(BorderFactory.createTitledBorder("你最喜欢哪种程序语言,喜欢的请打勾:"));
r4 = new JRadioButton("java", new ImageIcon(".\\icons\\x.jpg"));
r5 = new JRadioButton("c++", new ImageIcon(".\\icons\\x.jpg"));
r4.addItemListener(this);
r5.addItemListener(this);
p2.add(r4);
p2.add(r5);
ButtonGroup bgroup2 = new ButtonGroup();
bgroup2.add(r4);
bgroup2.add(r5);
contentPane.add(p1);
contentPane.add(p2);
f.pack();
f.show();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == e.SELECTED) {
if (e.getSource() == r4)
r4.setIcon(new ImageIcon(".\\icons\\r.jpg"));
if (e.getSource() == r5)
r5.setIcon(new ImageIcon(".\\icons\\r.jpg"));
} else {
if (e.getSource() == r4)
r4.setIcon(new ImageIcon(".\\icons\\x.jpg"));
if (e.getSource() == r5)
r5.setIcon(new ImageIcon(".\\icons\\x.jpg"));
}
}
public static void main(String[] args) {
new JRadioButton2();
}
}