解决方法
我强烈推荐阅读
this excellent tutorial.这是一篇文章的代码摘录,满足您如何创建和添加按钮到ButtonGroup的问题:
JRadioButton birdButton = new JRadioButton(birdString); birdButton.setSelected(true); JRadioButton catButton = new JRadioButton(catString); //Group the radio buttons. ButtonGroup group = new ButtonGroup(); group.add(birdButton); group.add(catButton);
至于选择哪个项目,您基本上需要iterate through the items in the group calling isSelected
.