如何检查Java GUI中复选框的状态?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1509682/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:38:13  来源:igfitidea点击:

How to check the status of checkbox in java GUI?

javauser-interfacecheckbox

提问by Yatendra Goel

I have around 200 hundred checkboxes in a Java GUI. Now I want to have the list of all checkboxes that have been checked by the user.

我在 Java GUI 中有大约 20000 个复选框。现在我想获得用户选中的所有复选框的列表。

I can do it in one way like this:

我可以用这样的一种方式做到这一点:

jCheckBox1.isSelected();

But I don't want to write this line for 200 checkboxes. Is there any way to do this through a for loop.

但我不想为 200 个复选框写这一行。有没有办法通过 for 循环来做到这一点。

all the checkboxes have the name like jCheckBox1, jCheckBox2, jCheckBox3, jCheckBox4 ... jCheckBox200

所有复选框的名称都类似于 jCheckBox1、jCheckBox2、jCheckBox3、jCheckBox4 ... jCheckBox200

采纳答案by Tom Martin

You really should have put these in an array or Collection so that you can just loop over them. eg.

你真的应该把它们放在一个数组或集合中,这样你就可以循环遍历它们。例如。

List<JCheckBox> allCheckBoxes = new ArrayList<JCheckBox>()
allCheckboxes.add(new JCheckBox());

etc.

等等。

If you have all these checkboxes declared as members then there's no excuse to just put them in a list instead.

如果您将所有这些复选框都声明为成员,那么就没有理由将它们放在列表中。

In the meantime you could use a dodgy cast in a for loop (if all the checkboxes are on the same panel)

同时,您可以在 for 循环中使用狡猾的演员表(如果所有复选框都在同一个面板上)

boolean allSelected = true;
for(Component component : myPanel.getComponents()) {
  if(component instanceof JCheckBox) {
    allSelected &= ((JCheckBox)component).isSelected();
  }
}

I'd recommend reading about Java arrays and collections before continuing

我建议在继续之前阅读有关 Java 数组和集合的信息

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html

http://java.sun.com/docs/books/tutorial/collections/index.html

http://java.sun.com/docs/books/tutorial/collections/index.html

回答by Malaxeur

Well, if you have your checkboxes in an array... you can do it in a loop.

好吧,如果您将复选框放在数组中……您可以循环进行。

JCheckBox[] myBoxes = new JCheckBox[200];

//Some creation stuff here

for (int i=0; i<myBoxes.length; ++i) {
   if (myBoxes[i].isSelected()) {
       //Do stuff!
   }
}

回答by NawaMan

Ummm, what should I say?

嗯,我该怎么说?

OK, You should starts by using array to hold all check boxes you have so you can loop through it.

好的,您应该首先使用数组来保存您拥有的所有复选框,以便您可以遍历它。

In case that is too late, you may have a another choice by loop though all elements on that container (only work if all checkboxes are on the same container). Something like 'jPanel1.getComponents()' and then loop them only see which is a Checkbox.

如果为时已晚,您可以通过循环选择另一个选择,尽管该容器上的所有元素(仅当所有复选框都在同一个容器上时才有效)。像 ' jPanel1.getComponents()' 之类的东西,然后循环它们只会看到哪个是复选框。

Anyway .. I think, you should put all those in an array and save yourself from a variable mess.

无论如何..我认为,您应该将所有这些放在一个数组中,并使自己免于一团糟。