监听 ButtonGroup 上的“child”更改,并打印选定的 JRadioButton 的文本
我想要的是: 创建一个事件,该事件在选择 ButtonGroup 中包含的 JRadioButton 时触发,然后打印 JRadioButton 上的文本。
What I want to is:
Create an event that fires if the a JRadioButton contained in the ButtonGroup is selected, and then print the text there is on the JRadioButton.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据我的评论,您无法向 ButtonGroup 添加侦听器。您可能必须将 ActionListener 添加到各个 JRadioButton。
如果这不能回答您的问题,请告诉我们有关您问题的更多详细信息。
编辑 1
我想您总是可以扩展 ButtonGroup 以便它接受 ActionListener。例如:
并通过以下测试:
但这最终仍然将 ActionListers 添加到每个 JRadioButton 以达到其目的;它只是在 MyButtonGroup 的 add 方法重写的幕后执行此操作。
As per my comment, you can't add a listener to a ButtonGroup. You will likely have to go with an ActionListener added to the individual JRadioButtons.
If this doesn't answer your question, please tell us more details about your problem.
Edit 1
I suppose you could always extend ButtonGroup such that it accepts ActionListeners. For example:
And tested by the following:
But this still eventually adds ActionListers to each JRadioButton to achieve its ends; it just does this behind the scenes in the MyButtonGroup's add method override.
与接受的答案类似的方法,尽管我更喜欢通过 index 控制 ButtonGroup 并检索选择 index 。
已应用:
注意: @wbp.parser.entryPoint 是 Eclipse WindowBuilder PlugIn 的改编版本
Similar approach as accepted answer, though I prefer to control a ButtonGroup by index and retrieve a selection index .
Applied:
Note: @wbp.parser.entryPoint is an adaption for the Eclipse WindowBuilder PlugIn