如何检查java GUI中的复选框的状态? [英] How to check the status of checkbox in java GUI?

查看:864
本文介绍了如何检查java GUI中的复选框的状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我可以这样做:

jCheckBox1.isSelected();

但我不想写这行200个复选框。有任何方法可以通过一个for循环。

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

所有的复选框都有名称像jCheckBox1,jCheckBox2,jCheckBox3,jCheckBox4 ... jCheckBox200

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

推荐答案

你真的应该把这些放在一个数组或集合中,以便你可以循环。例如

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

等。

这些复选框声明为成员,然后没有借口,只是把它们放在一个列表中。

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

在此期间,你可以在一个for循环使用一个dodgy强制转换复选框在同一个面板上)

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

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

我建议先阅读Java数组和集合继续

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

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

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

这篇关于如何检查java GUI中的复选框的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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