Java 检查复选框是否被选中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27423485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java check if Checkbox is checked
提问by
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
......同样,如果选中一个复选框,我该如何检查?
采纳答案by NiziL
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");
}
回答by NiziL
You can use Checkbox::getState()
or (as said in the comment) CheckboxGroup#getSelectedCheckbox()
您可以使用Checkbox::getState()
或(如评论中所述)CheckboxGroup#getSelectedCheckbox()
回答by No Idea For Name
judging by your use of isSelected
i concluded you have 1 of 2 mistakes:
根据您对isSelected
i的使用判断,您有 2 个错误中的 1 个:
- you want to use check box, if that is the case, then you should use
c1.getState()
and notisSelected()
- you need
RadioBox
instead ofCheckBox
and then you can use theisSelected()
method. check here about the two
- 你想使用复选框,如果是这样,那么你应该使用
c1.getState()
而不是isSelected()
- 你需要
RadioBox
而不是CheckBox
然后你可以使用该isSelected()
方法。 检查这里关于这两个
回答by Madhuka Dilhan
you can try this code
你可以试试这个代码
// check is ckeck box id
if (check.isSelected()) {
// your code for checked;
} else {
// our code for not checked;
}
回答by Ram Satya
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; but not CheckBox of AWT...
如果您使用 javax.swing.JComponent 中的 JCheckBox,则可以使用第二个 .isSelected() 方法;但不是 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
回答by horoserp
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
}