我有一个包含 5 个单选按钮的按钮组,我需要检查是否至少选择了其中一个。
这是正确的吗?
buttonGroup1.isSelected(ButtonModel);
但是ButtonmModel
这里有什么?
我有一个包含 5 个单选按钮的按钮组,我需要检查是否至少选择了其中一个。
这是正确的吗?
buttonGroup1.isSelected(ButtonModel);
但是ButtonmModel
这里有什么?
buttonGroup1.getSelection()==null
此外,更重要的是,您可以使用 ButtonGroup 类的 getSelected() 方法确定按钮组的单选按钮之间是否存在选择,因为如果未选择任何内容,它将返回 null。
private boolean isSelection(ButtonGroup buttonGroup) {
return (buttonGroup.getSelected() != null);
}
我希望这可以帮助别人!!!
由于您只有 5 个单选按钮,您可以手动检查所有单选按钮(假设单选按钮命名为 radio1、...、radio5):
boolean isRadio1, isRadio2, isRadio3, isRadio4, isRadio5 = false;
if (radio1.isSelected() == true) isRadio1 = true;
if (radio2.isSelected() == true) isRadio2 = true;
if (radio3.isSelected() == true) isRadio3 = true;
if (radio4.isSelected() == true) isRadio4 = true;
if (radio5.isSelected() == true) isRadio5 = true;
不是最优雅的解决方案,但它会成功。
package radiobuttongroup;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
public class RadioButtonGroup {
public static void main(String[] args) {
new RadioButtonGroup();
}
private RadioButtonGroup() {
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(0, 1));
for (int i = 1; i <= 5; i++) {
JRadioButton radio = new JRadioButton(Integer.toString(i));
ButtonModel buttonModel = radio.getModel();
modelToRadioButton.put(buttonModel, radio);
buttonGroup.add(radio);
contentPane.add(radio);
}
JButton buttonTestSelection = new JButton("Selection Test");
JButton buttonClearSelection = new JButton("Clear Selection");
contentPane.add(buttonTestSelection);
contentPane.add(buttonClearSelection);
buttonTestSelection.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent arg0) {
ButtonModel buttonModel = buttonGroup.getSelection();
if (buttonModel == null) {
System.out.println("No radio button is selected");
}
else {
if (modelToRadioButton.containsKey(buttonModel)) {
JRadioButton b = modelToRadioButton.get(buttonModel);
System.out.println("You selected button " + b.getText());
}
else {
System.err.println("Weird, unrecognised button model!");
}
}
}
});
buttonClearSelection.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
buttonGroup.clearSelection();
}
});
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JFrame frame = new JFrame("RadioButtonGroup");
private Map<ButtonModel, JRadioButton> modelToRadioButton =
new LinkedHashMap<ButtonModel, JRadioButton>();
private ButtonGroup buttonGroup = new ButtonGroup();
}
在查找选定的单选按钮时,您无法通过 ButtonModel 做很多事情。但是 ButtonGroup 确实具有返回 AbstractButtons 枚举的 getElements() 方法。这些 AbstractButtons 可以在迭代期间通过枚举转换为 JRadioButtons...
以下方法将返回传递给它的ButtonGroup的选定JRadioButton,如果没有选择则返回null...
private JRadioButton getSelectedRadioButton(ButtonGroup buttonGroup) {
Enumeration<AbstractButton> abstractButtons = buttonGroup.getElements();
JRadioButton radioButton = null;
while (abstractButtons.hasMoreElements()) {
radioButton = (JRadioButton) abstractButtons.nextElement();
if (radioButton.isSelected()) {
break;
}
}
return radioButton;
}
一旦您选择了 JRadioButton,就可以访问它的属性。假设您想要选择任何按钮的文本属性...
String selectedRadioButtonText = getSelectedRadioButton(buttonGroup).getText();
我希望这可以帮助别人!!!