//单选按钮类JRadioButton
package jradiobutton;
import javax.swing.*;
public class TestRedioButton extends JFrame {
JRadioButton r1 = new JRadioButton("南京",true);
JRadioButton r2 = new JRadioButton("北京",false);
JRadioButton r3 = new JRadioButton("上海",false);
TestRedioButton(){
super("选择喜欢的城市,只能一个呀!");
this.setSize(100,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ButtonGroup b = new ButtonGroup();
b.add(r1);
b.add(r2);
b.add(r3);
JPanel p = new JPanel();
p.add(r1);
p.add(r2);
p.add(r3);
this.setContentPane(p);
}
public static void main(String[] args) {
TestRedioButton t = new TestRedioButton();
t.show();
}
}
import javax.swing.*;
public class TestRedioButton extends JFrame {
JRadioButton r1 = new JRadioButton("南京",true);
JRadioButton r2 = new JRadioButton("北京",false);
JRadioButton r3 = new JRadioButton("上海",false);
TestRedioButton(){
super("选择喜欢的城市,只能一个呀!");
this.setSize(100,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ButtonGroup b = new ButtonGroup();
b.add(r1);
b.add(r2);
b.add(r3);
JPanel p = new JPanel();
p.add(r1);
p.add(r2);
p.add(r3);
this.setContentPane(p);
}
public static void main(String[] args) {
TestRedioButton t = new TestRedioButton();
t.show();
}
}