How to manage two JRadioButtons in java so that only one of them can be selected at a time

别来无恙 提交于 2019-11-29 11:48:39

问题


How to manage two JRadioButtons in java so that only one of them can be selected at a time? Is there any method in java to take care of this or you need to build your own logic?


回答1:


You have to add them in a ButtonGroup

ButtonGroup group = new ButtonGroup();
group.add(birdButton);
group.add(catButton);

Ensure you add this code after the buttons are created using the new JRadioButton constructors, as appropriate.




回答2:


My java is rusty but if i remember correctly you have to use the ButtonGroup class. Add your radio buttons to ButtonGroup object. I think it will look like this.

ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(new JRadioButton('Label', false));
buttonGroup.add(new JRadioButton('Label2', true));

Hope this helps. I have abandoned Java years ago.



来源:https://stackoverflow.com/questions/2253590/how-to-manage-two-jradiobuttons-in-java-so-that-only-one-of-them-can-be-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!