import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Answer6_2 extends Frame implements ActionListener{
public Answer6_2(){
  CheckboxGroup cg = new CheckboxGroup();
      Checkbox c1 = new Checkbox("启用",cg,false);
  Checkbox c2 = new Checkbox("禁用",cg,false);
this.setLayout(new GridLayout(1,2));
c1.addActionListener(this);
c2.addActionListener(this);
this.add(c1);this.add(c2);
this.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});
this.setSize(350,200);
    this.setVisible(true);
}
public static void main(String args[]){
     Answer6_2 f = new Answer6_2();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==c1) f.setTitle("启用第一个单选按钮");
else f.setTitle("启用第二个单选按钮");
}
}里面凡是含有c1  c2的语句都说找不到符号PS:如果用JFrame呢?JCheckbox,好像单选按钮没有JChecboxGroup,得用什么JRadioButton 
求解答

解决方案 »

  1.   

    JRadioButton用法JRadioButton radioButton1 = new JRadioButton("启用");
        
    radioButton1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if(radioButton1.isSelected()){
                                                   radioButton.setSelected(false);//
    radioButton1.setSelected(true);
    }

    }
    });
    JRadioButton radioButton = new JRadioButton("启用");
        
    radioButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if(radioButton.isSelected()){
                                                   radioButton.setSelected(true);//
    radioButton1.setSelected(false);
    }

    }
    });
      

  2.   

    现在都改用JFrame 了,JCheckBox 设置多选, JRadioButton也是选择按钮, 用
    JRadioButton b1 = new JRadioButton("");
    ButtonGroup bg = new ButtonGroup();
    bg.add(b1); 放在一个ButtonGroup里面, 实现单选.
      

  3.   

    1楼的:你是不是没有添加Item的监听器啊?
    2楼:JCheckBox是不是和JRadioButton等价?
      

  4.   

    自己解决的,c1,c2应该在class Answer6_2中声明,而不是在public Answer6_2中声明,因为会成为局部变量
    ActionListener中的f.set.Title()都改成this.Title()
      

  5.   


    JCheckBox是不是和JRadioButton等价?
      

  6.   

    checkboxGroup有一个方法getSelectedCheckbox()能获得当前选中项,ButtonGroup似乎没有,就这点不好
      

  7.   


    我用的是getSource,获取对象源