我使用:
CheckboxGroup cg = new CheckboxGroup();
Checkbox c1 = new Checkbox("A", false, cg);
Checkbox c2 = new Checkbox("B", false, cg);
Checkbox c3 = new Checkbox("C", true, cg);
创建一组三个复选框。现在,我想检查检查了其中的哪一个。我使用:
if (c1.isSelected()) { }
但是这给出了 The method isSelected() is undefined for the type Checkbox
...推荐的解决方案是将 cast 添加到 c1,我这样做了并且它给出了 Cannot cast from Checkbox to AbstractButton
...同样,我如何才能检查复选框是否已选中?
最佳答案
使用 getState()
boolean checked = c1.getState();
if(c1.getState()) {
//c1 is checked
} else if (c2.getState()) {
//
}
或
Checkbox cb = cg.getSelectedCheckbox();
if(null != cb) {
//not checked
} else {
System.out.println(cb.getLabel() + " is checked");
}
关于Java 检查复选框是否被选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27423485/