如何计算检查的 JCheckbox 数量? [英] How to count number of JCheckboxes checked?

查看:25
本文介绍了如何计算检查的 JCheckbox 数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 JFrame 中有 11 个不同的复选框,并且希望能够在每次检查一个检查总数时获得一个数字.我知道如何设置一个 ItemListener 并查看是否选中了一个,但我不确定如何检查所有这些..

I have 11 different checkboxes in my JFrame and want to be able to get a number whenever one is checked for how many total are checked. I know how to set up an ItemListener and see if one is checked, but I am not sure how I could check all of them..

cblist 是一个包含 11 个 JCheckBox 的 ArrayList.我给每个 JCheckBox 一个项目监听器,这是单击复选框时使用的类...

cblist is an ArrayList containing 11 JCheckBoxes. I gave every JCheckBox an item listener and hereis the class used when the checkboxes are clicked...

private class CheckClass implements ItemListener{
      public void itemStateChanged(ItemEvent event){
         for(cblist.isChecked){
             ingnum++;
         }

      }
  }

在 for 循环中,如何测试 ArrayList 的所有元素.我知道我现在的语法不正确.

In the for loop, how do I test all elements of the ArrayList..I understand my syntax is not correct right now.

推荐答案

一种方法:将所有 JCheckBox 放入一个数组或 ArrayList 并在需要时简单地遍历列表以查看选中了哪些复选框.

One way: put all of the JCheckBoxes in an array or ArrayList<JCheckBox> and when desired, simply iterate through the list to see which check boxes are selected.

另一种可能的解决方案:如果您有一个表格结构,请使用在其模型中保存布尔值的 JTable,然后在需要时遍历 TableModel 的行以查看哪些行保存 Boolean.TRUE 值.

Another possible solution: if you have a tabular structure, use a JTable that holds Booleans in its model, then when desired iterate through the rows of the TableModel to see which rows hold Boolean.TRUE values.

这篇关于如何计算检查的 JCheckbox 数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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