1

我有两个用 java 制作的单选按钮。问题是它们没有链接在一起,即它们都可以同时被选中。我如何实现它们之间的链接?

4

2 回答 2

3

我认为你需要这个;

        //Create three radio buttons   
        JRadioButton aButton = new JRadioButton("A",true);
        JRadioButton bButton = new JRadioButton("B");
        JRadioButton cButton = new JRadioButton("C");

        //Create a ButtonGroup object, add buttons to the group
        ButtonGroup myButtonGroup = new ButtonGroup();
        myButtonGroup.add(aButton);
        myButtonGroup.add(bButton);
        myButtonGroup.add(cButton);

        //Display radio buttons
        getContentPane().setLayout(new FlowLayout());
        getContentPane().add(aButton);
        getContentPane().add(bButton);
        getContentPane().add(cButton);
        setSize(250,100);
        setTitle("Swing Radio Buttons");
        setVisible(true);

让我知道,如果有帮助。

于 2013-07-27T18:00:59.077 回答
-2

设置相同name属性 Radio 示例

<input id="hello" type="radio" name="greet">
<label for="hello">hello</label>
<input id="hi" type="radio" name="greet">
<label for="hi">hi</label>
<hr />
<input id="all" type="radio" name="who">
<label for="all">world!</label>
<input id="one" type="radio" name="who">
<label for="one">you</label>
于 2013-07-27T17:15:01.197 回答