This question already has an answer here:
这个问题在这里已有答案:
- How to count number of JCheckboxes checked? 4 answers
- 如何计算JCheckboxes的数量? 4个答案
I was wondering if there was a way to check the number of checkboxes checked in Java. I found plenty of instruction on how to do in in javascript but not Java. Thanks for you help!
我想知道是否有办法检查用Java检查的复选框的数量。我发现了很多关于如何使用javascript而不是Java的指令。谢谢你的帮助!
1 个解决方案
#1
1
A few options that I can think of-
我能想到的一些选择 -
-
Every time a checkbox is checked, increment a (global) counter in its listener. Decrement the counter when the checkbox is un-checked. So, at any given point in time, you know the number of checkboxes checked
每次选中复选框时,在其侦听器中递增(全局)计数器。取消选中复选框后,递减计数器。因此,在任何给定的时间点,您都知道选中的复选框的数量
-
Iterate through all the checkboxes and check how many of them are checked. It would be good if you create a collection of checkboxes that you would be using in your code
遍历所有复选框并检查其中检查了多少个复选框。如果您创建一个您将在代码中使用的复选框集合,那将是一件好事
-
If the checkboxes are on different screens or in different classes, then you need to main counter at all places and sum them up whenever you want to know the number of checkboxes checked in your application
如果复选框位于不同的屏幕上或不同的类别中,那么您需要在所有位置使用主计数器并在您想知道应用程序中选中的复选框数量时总结它们
#1
1
A few options that I can think of-
我能想到的一些选择 -
-
Every time a checkbox is checked, increment a (global) counter in its listener. Decrement the counter when the checkbox is un-checked. So, at any given point in time, you know the number of checkboxes checked
每次选中复选框时,在其侦听器中递增(全局)计数器。取消选中复选框后,递减计数器。因此,在任何给定的时间点,您都知道选中的复选框的数量
-
Iterate through all the checkboxes and check how many of them are checked. It would be good if you create a collection of checkboxes that you would be using in your code
遍历所有复选框并检查其中检查了多少个复选框。如果您创建一个您将在代码中使用的复选框集合,那将是一件好事
-
If the checkboxes are on different screens or in different classes, then you need to main counter at all places and sum them up whenever you want to know the number of checkboxes checked in your application
如果复选框位于不同的屏幕上或不同的类别中,那么您需要在所有位置使用主计数器并在您想知道应用程序中选中的复选框数量时总结它们