简体   繁体   English

Java 检查复选框是否被选中

[英]Java check if Checkbox is checked

I use:我用:

    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()) { }

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?但这给出了The method isSelected() is undefined for the type Checkbox ...推荐的解决方案是将强制转换添加到 c1,我这样做了,它给出了Cannot cast from Checkbox to AbstractButton ......同样,我怎么能只检查一个 Checkbox如果检查?

Use getState()使用 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");
}

您可以使用Checkbox::getState()或(如评论中所述) CheckboxGroup#getSelectedCheckbox()

1st of all java.awt.Checkbox doesn't have .isSelected() method in its super class, which is java.awt.Component.第一个 java.awt.Checkbox 在其超类中没有 .isSelected() 方法,即 java.awt.Component。

https://docs.oracle.com/javase/7/docs/api/java/awt/Checkbox.html https://docs.oracle.com/javase/7/docs/api/java/awt/Checkbox.html

please check the above link for Methods inherited from class java.awt.Component请检查上面的链接从类 java.awt.Component 继承的方法

2nd .isSelected() method can be used if you use JCheckBox from javax.swing.JComponent;如果您使用 javax.swing.JComponent 中的 JCheckBox,则可以使用第二个 .isSelected() 方法; but not CheckBox of AWT...但不是 AWT 的 CheckBox ......

please go through below link.. and you can find .isSelected() which is inherited from javax.swing.AbstractButton;请通过下面的链接.. 你可以找到从 javax.swing.AbstractButton 继承的 .isSelected();

https://docs.oracle.com/javase/7/docs/api/javax/swing/JCheckBox.html https://docs.oracle.com/javase/7/docs/api/javax/swing/JCheckBox.html

I found the isChecked() method to be the winner.我发现 isChecked() 方法是赢家。

 // Check to see if box is checked
 if (c1.isChecked()) {
      // Your code if the box is checked
 } else {
      // Your code if the box is not checked
 }

judging by your use of isSelected i concluded you have 1 of 2 mistakes:根据您对isSelected的使用判断,我得出结论您有 2 个错误中的 1 个:

  1. you want to use check box, if that is the case, then you should use c1.getState() and not isSelected()你想使用复选框,如果是这种情况,那么你应该使用c1.getState()而不是isSelected()
  2. you need RadioBox instead of CheckBox and then you can use the isSelected() method.您需要RadioBox而不是CheckBox ,然后您可以使用isSelected()方法。 check here about the two 检查这里关于这两个

you can try this code你可以试试这个代码

// check is ckeck box id
if (check.isSelected()) {
           // your code for checked;
 } else {
           // our code for not checked;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM