给你写个简单的例子:
public class TestJFrame extends JFrame implements ActionListener{
ButtonGroup group;
JRadioButton smallButton;
JRadioButton largeButton;
JButton jbtopic; // 建议你的“题目”用Button控件
public TestJFrame (){
this.setVisible(true);
jbtopic = new JButton("题目");
jbtopic.addActionListener(this); //给jbtopic 按钮注册监听
group = new ButtonGroup();
smallButton = new JRadioButton("Small",true);
largeButton = new JRadioButton("Large",false);
group.add(smallButton );
group.add(largeButton );
this.setLayout(new GridLayout(2,1)); //设置布局方式为网格布局,行列数自己改一下
this.add(smallButton);
this.add(largeButton);
this.add(jbtopic);
}
public static void main(String[] args) {
TestJFrame tjf = new TestJFrame();
}
public void actionPerformed(ActionEvent e) {
if( e.getActionCommand().equals("题目") ){
if(smallButton.isSelected()) {
System.out.println("small");
}
if(largeButton.isSelected()) { //用isSelected返回true,或者false
System.out.println("large");
}
}
}