2013-07-27 126 views
1

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

+3

你需要阅读有关单选按钮组。顺便说一下你在用什么方法? –

+0

嘿,想知道下面的答案是否对你有帮助? – JNL

回答

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); 

让我知道,如果帮助。

+0

它解决了问题 –

+0

当然,很高兴它帮助。我很感激,如果你能接受答案,如果它帮助你。 TX – JNL

-2

设置相同name属性 Radio example

<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> 
+0

我在java中说过,你提的是html –

+0

猜你在说java swing! –

+0

在这种情况下,您正在寻找这个http://docs.oracle.com/javase/7/docs/api/javax/swing/ButtonGroup.html – ep0