Java检查是否选中了Checkbox [英] Java check if Checkbox is checked

查看:694
本文介绍了Java检查是否选中了Checkbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用:

    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);

创建一组三个复选框。现在,我想检查哪一个被检查。我使用:

To create a group of three checkboxes. Now, I want to check which one of them is checked. I use:

if (c1.isSelected()) { }

但是这给出了方法isSelected()未定义类型Checkbox ...建议的解决方案是添加转换为c1,我这样做,它给出了无法从Checkbox转换为AbstractButton ...再次,如何选中Checkbox是否可以检查?

but this gives The method isSelected() is undefined for the type Checkbox... Recommended solution is add cast to c1, I do so and it gives Cannot cast from Checkbox to AbstractButton... Again, how can I just check if a Checkbox if checked?

推荐答案

使用getState()

Use getState()

boolean checked = c1.getState();
if(c1.getState()) {
  //c1 is checked
} else if (c2.getState()) {
  //
}

OR

Checkbox cb = cg.getSelectedCheckbox();
if(null != cb) {
  //not checked
} else {
  System.out.println(cb.getLabel() + " is checked");
}

这篇关于Java检查是否选中了Checkbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆