【发布时间】:2018-10-01 16:33:56
【问题描述】:
我有这个数组:
String extras[] = new String[6];
extras[0] = "bacon";
extras[1] = "potato";
extras[2] = "chicken";
extras[3] = "cheese";
extras[4] = "olive";
extras[5] = "tomato";
还有这个数组列表:
ArrayList selectedExtra = new ArrayList();
我试图只获取选定的项目,所以我有这个 for 循环:
for (int i=0; i<extras.length ; i++){
String aditional = extras[i]; //get each extra name
if(aditional.isSelected()){ // thats my problem
selectedExtra.add(adicional); // add the selected extras
}
}
我正在尝试为每个字符串赋予属性,比如说“Bacon”,并使用它来检查是否选择了具有该名称的 JCheckBox,如果是,则将其归因于我的 arrayList。
是的,数组项的名称与 JCheckBox 的名称完全相同。
我怎样才能完成它?谢谢!
【问题讨论】:
-
不是答案,但您应该避免使用原始类型。试试
ArrayList<String> selectedExtra = new ArrayList<>();
标签: java arrays swing jcheckbox