java checkbox 状态,如何检查java GUI中的复选框的状态?

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.

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.

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 allCheckBoxes = new ArrayList()

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)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值