如何在 Java 中使用 Button Group Swing 控件?
如何使用 NetBeans 将单选按钮添加到按钮组?
添加它们后,如何从按钮组中选择选定的单选按钮?
How do I add radio buttons to a button group using NetBeans?
Once I add them, how do I get selected radio button from the button group?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
ButtonGroup
并将其放到 GUI 上。它将显示在检查器面板中的其他组件下。
ButtonGroup
from the palette and drop it on your GUI.It will show up under Other Components in the Inspector panel.
我强烈建议阅读这篇优秀的教程。以下是文章中的代码摘录,它满足了您有关如何创建按钮并将按钮添加到 ButtonGroup 的问题:
至于获取选择的项目,您基本上需要 迭代调用
isSelected
的组中的项目。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:
As far as getting which item is selected, you basically need to iterate through the items in the group calling
isSelected
.要以编程方式选择单选按钮,请尝试以下操作:
To select a radio button programmatically, try these:
如何使用按钮、复选框和单选按钮
How to Use Buttons, Check Boxes, and Radio Buttons
在导航器窗格中的“其他组件”下,选择您的按钮组。然后在“属性”窗格中选择“代码”选项卡。选择省略号 (...) 以编辑“After-All-Set Code”部分。输入代码以将按钮添加到按钮组,如上所述。
例如:
attemptGroup.add(attemptRadio1);
tryGroup.add(attemptRadio2);
attemptsGroup.add(attemptRadio3);
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.
For example:
attemptGroup.add(attemptRadio1);
attemptGroup.add(attemptRadio2);
attemptGroup.add(attemptRadio3);