再会

想知道如何通过将组件拖放到框架上来将单选按钮添加到ButtonGroup中。

JRadioButton male = new JRadioButton("Male");
JRadioButton female = new JRadioButton("Female");
ButtonGroup bg = new ButtonGroup();
bg.add(male);
bg.add(female);
pane.add(male);
pane.add(female);

我如何通过简单的拖放来做到这一点。如果将ButtonGroup放到框架上,它将被归类到“其他组件”下,并且从那里我不确定如何将单选按钮添加到按钮组中。

谢谢
问候阿里安

最佳答案

您查看了HierarchyListener,在hierarchyChanged事件上,向JRadioButton中添加了一个新的ButtonGroup,最后一次为具体的JRadioButton选择了isSelected

10-08 16:18