How to count number of JCheckboxes checked?(如何计算检查的 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 数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何计算检查的 JCheckbox 数量?


- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 转换 ldap 日期 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 获取数字的最后一位 2022-01-01