(Java)如何使用for生成的复选框

时间:2020-12-25 03:16:43

I have a Problem, I generate some checkbox with a for and i need to use them but don´t know how they are named to call them.

我有一个问题,我生成一个带有for的复选框,我需要使用它们但不知道如何命名它们来调用它们。

for(disponibility prov: dis){

JCheckBox check=new JCheckBox();

check.setBounds(30,156,97,33);

}

1 个解决方案

#1


1  

Just use an array list type of structure to store all your checkboxes. Access them using an index later. If you want to name them based on some property of disponibility, you can use a map to store the checkboxes.

只需使用数组列表类型的结构来存储所有复选框。稍后使用索引访问它们。如果要根据某些可用性属性对它们进行命名,可以使用地图来存储复选框。

List<JCheckBox> list = new ArrayList<>();
for(disponibility prov: dis){    
   JCheckBox check=new JCheckBox();    
   check.setBounds(30,156,97,33);
   list.add(check);
}
//Later access nth checkbox
list.get(n).setEnabled(true);

#1


1  

Just use an array list type of structure to store all your checkboxes. Access them using an index later. If you want to name them based on some property of disponibility, you can use a map to store the checkboxes.

只需使用数组列表类型的结构来存储所有复选框。稍后使用索引访问它们。如果要根据某些可用性属性对它们进行命名,可以使用地图来存储复选框。

List<JCheckBox> list = new ArrayList<>();
for(disponibility prov: dis){    
   JCheckBox check=new JCheckBox();    
   check.setBounds(30,156,97,33);
   list.add(check);
}
//Later access nth checkbox
list.get(n).setEnabled(true);