GUI

好了,对了请问怎么让jRadioButton,在点击后回到未点击的状态呢?

解决方案 »

  1.   

    group.clearSelection();public class RadioButtonTest extends JFrame {
    JButton jbt = new JButton("重置");
    JRadioButton[] jrbts = new JRadioButton[4];
    ButtonGroup group;
    public RadioButtonTest(){
    setTitle("RadioButtonTest");
    setSize(400,200);
    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e){
    System.exit(0);
    }
    });
    JPanel buttonPanel = new JPanel();
    group = new ButtonGroup();
    for(int i = 0;i < jrbts.length ;i ++){
    jrbts[i] = new JRadioButton();
    addRadioButton(buttonPanel,group,"jrbts["+i+"]",false);
    }
    getContentPane().add(buttonPanel,"South");
    jbt.addActionListener(new MyListener());
    getContentPane().add(jbt,"Center");
    } class MyListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
    group.clearSelection();
    }
    } public JRadioButton addRadioButton(JPanel buttonPanel,ButtonGroup g,String buttonName,boolean v){
    JRadioButton button = new JRadioButton(buttonName,v);
    g.add(button);
    buttonPanel.add(button);
    return button;
    } public static void main(String[] args){
    JFrame frame = new RadioButtonTest();
    frame.setVisible(true);
    }
    }