关于Java中JRadioButton的用法?

解决方案 »

  1.   

    托一个 ButtonGroup
    在把 JRadioButton 添加到 ButtonGroup 里就 OK了
    这样 JRadioButton 就互斥了下面是我用JB 做的简单例子
    =======================
    public class Frame1 extends JFrame {
        public Frame1() {
            try {
                jbInit();
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }    private void jbInit() throws Exception {
            getContentPane().setLayout(null);
            jRadioButton1.setText("jRadioButton1");
            jRadioButton1.setBounds(new Rectangle(77, 81, 103, 23));
            this.getContentPane().add(jRadioButton2);
            this.getContentPane().add(jRadioButton1);
            jRadioButton2.setText("jRadioButton2");
            jRadioButton2.setBounds(new Rectangle(216, 80, 103, 23));
            this.buttonGroup1.add(jRadioButton1);
            this.buttonGroup1.add(jRadioButton2);
            this.setSize(400,300);
            this.setVisible(true);
        }
        public static void main(String[] args) {
            Frame1 f = new Frame1();
           
        }
        ButtonGroup buttonGroup1 = new ButtonGroup();
        JRadioButton jRadioButton1 = new JRadioButton();
        JRadioButton jRadioButton2 = new JRadioButton();
    }
      

  2.   

    Java Tutorial讲得很清楚的。
    http://java.sun.com/docs/books/tutorial/uiswing/components/button.html#radiobutton