Java 将 ButtonGroup 添加到 JPanel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18941840/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:43:08  来源:igfitidea点击:

Add ButtonGroup to JPanel

javaswingjpanelawtbuttongroup

提问by Zarkopafilis

JPanel.add(ButtonGroup);

Is not working. I MUST add it to a JPanel because I am using tabs. This is getting really frustrating.I hace not found a way yet

不管用。我必须将它添加到 JPanel,因为我使用的是选项卡。这真是令人沮丧。我还没有找到方法

采纳答案by Manas Mukherjee

As ButtonGroup is not a component, you cannot add your ButtonGroup to a JPanel. Instead add your buttons to your JPanel, e.g:

由于 ButtonGroup 不是组件,因此您无法将 ButtonGroup 添加到 JPanel。而是将您的按钮添加到您的 JPanel,例如:

JPanel jPanel = new JPanel();
ButtonGroup group = new ButtonGroup();
btn1 = new JRadioButton("btn1 ");btn1.setSelected(true);
btn2 = new JRadioButton("btn2 ");
group.add(btn1 );
group.add(btn2 );

jPanel.add(btn1);
jPanel.add(btn2).

Hope it will be useful.

希望它会很有用。

Thanks

谢谢