如何在 Java 中使用按钮组 Swing 控件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2301123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I use the Button Group Swing control in Java?
提问by Venkat
How do I add radio buttons to a button group using NetBeans?
如何使用 NetBeans 将单选按钮添加到按钮组?
Once I add them, how do I get selected radio button from the button group?
添加它们后,如何从按钮组中获取选定的单选按钮?
采纳答案by Mark Elliot
I highly recommend reading this excellent tutorial. Here's an excerpt of code from the article that satisfies your question on how to create and add buttons to a ButtonGroup:
我强烈推荐阅读这个优秀的教程。以下是文章中的代码摘录,可满足您关于如何创建按钮并将其添加到 ButtonGroup 的问题:
JRadioButton birdButton = new JRadioButton(birdString);
birdButton.setSelected(true);
JRadioButton catButton = new JRadioButton(catString);
//Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(birdButton);
group.add(catButton);
As far as getting which item is selected, you basically need to iterate through the items in the group calling isSelected
.
至于得到哪个item被选中,基本上需要遍历group call中的itemsisSelected
。
回答by twodayslate
How to Use Buttons, Check Boxes, and Radio Buttons
ButtonGroup group = new ButtonGroup();
group.add(new JRadioButton("one"));
group.add(new JRadioButton("two"));
//TO FIND SELECTED
//use a loop on group.getElements();
//and check isSelected() and add them
//to some sort of data structure
回答by Devon_C_Miller
- Drag a
ButtonGroup
from the palette and drop it on your GUI. It will show up under Other Componentsin the Inspectorpanel. - Right-click on it and Change variable nameto something meaningful.
- Now select a radio button in your GUI.
- In the Propertiespanel look for the buttonGroupproperty.
- Click the combo box next to it and select your button group.
ButtonGroup
从调色板中拖动 a并将其放在您的 GUI 上。它将显示在“检查器”面板中的“其他组件”下。- 右键单击它并将变量名称更改为有意义的名称。
- 现在在您的 GUI 中选择一个单选按钮。
- 在“属性”面板中查找buttonGroup属性。
- 单击它旁边的组合框并选择您的按钮组。
回答by NixRam
To select a radio button programmatically, try these:
要以编程方式选择单选按钮,请尝试以下操作:
private final ButtonGroup buttonGroup = new ButtonGroup();
JRadioButton btn01 = new JRadioButton("btn 1");
buttonGroup.add(btn01);
JRadioButton btn02 = new JRadioButton("btn 2");
buttonGroup.add(btn02);
JRadioButton btn03 = new JRadioButton("btn 3");
buttonGroup.add(btn03);
// gets the selected radio button
if(buttonGroup.getSelection().equals(btn01.getModel())) {
// code
}
// similarly for the other radio buttons as well.
回答by Tatyana Yufereva
In your Navigator Pane, under "Other Components", select your button group. Then select the Code tab in the Properties pane. Select the ellipses (...) to edit the section "After-All-Set Code". Enter your code to add buttons to the button group as previously explained above.
在您的导航器窗格中的“其他组件”下,选择您的按钮组。然后在“属性”窗格中选择“代码”选项卡。选择省略号 (...) 以编辑“After-All-Set Code”部分。如上所述,输入您的代码以将按钮添加到按钮组。
For example:
例如:
attemptGroup.add(attemptRadio1);
attemptGroup.add(attemptRadio2);
attemptGroup.add(attemptRadio3);
attemptGroup.add(attemptRadio1);
attemptGroup.add(attemptRadio2);
attemptGroup.add(attemptRadio3);
回答by user11205009
private final ButtonGroup agreeDisagree = new ButtonGroup();
JToggleButton tglbtnAgree = new JToggleButton("Agree");
tglbtnAgree.setSelected(true);
tglbtnAgree.setBounds(227, 127, 75, 23);
agreeDisagree.add(tglbtnAgree);
contentPane.add(tglbtnAgree);
JToggleButton tglbtnDisagree = newJToggleButton("Disagree");
tglbtnDisagree.setBounds(307, 127, 75, 23);
agreeDisagree.add(tglbtnDisagree);
contentPane.add(tglbtnDisagree);