JComboBox 未返回预期结果
JComboBox not returning expected result
public void actionPerformed(ActionEvent e) {
String[] arr = {tf1.getText(),tf2.getText(),"","","","","","","","","","","","","","","",""};
switch(cb1.getSelectedItem().toString()){
case "Hillsborough":
arr[2] = "1";
break;
case "Pinellas":
System.out.println("HIT 1");
arr[3] = "1";
break;
case "Pasco":
System.out.println("HIT 2");
arr[4] = "1";
break;
case "Hernando":
System.out.println("HIT 3");
arr[5] = "1";
break;
case "Polk":
System.out.println("HIT 3");
arr[6] = "1";
break;
case "Manatee":
System.out.println("HIT 4");
arr[7] = "1";
break;
case "Sarasota":
System.out.println("HIT 5");
arr[8] = "1";
break;
case "Other Florida":
System.out.println("HIT 6");
arr[9] = "1";
break;
case "Other State/Country":
System.out.println("HIT 7");
arr[10] = "1";
break;
default:
break;
}
switch(cb2.getSelectedItem().toString()){
case "Active Duty":
arr[11] = "1";
break;
case "Coalition Forces":
arr[12] = "1";
break;
case "Dependent":
arr[13] = "1";
break;
case "Guard/Reserve":
arr[14] = "1";
break;
case "Retired":
arr[15] = "1";
break;
case "Veteran":
arr[16] = "1";
break;
case "Civilian":
arr[17] = "1";
break;
default:
break;
}
reader.submit(2, arr);
for(int c = 0; c < arr.length;c++)
System.out.print(arr[c]);
}
When i click the confirm button it triggers this. It, depending on the selected result, will add a 1 to a specific spot in the array and the pass it on through another method. It has nothing to do with the second method, the results aren't being accessed at all. The first result in the combo box doesn't have a switch/case associated with it because those are the placeholders for the selction boxes. Any way to fix it?
And what return cbo.getSelectedItem().toString()?
使用列表而不是数组。列表是动态的,因此您可以在需要时添加元素。当您不知道数据的长度时使用数组是不可读的和不必要的。
List<String> data = new ArrayList<>();
data.add(cbo.getSelectedItem().toString());
您确定添加的是字符串而不是对象吗?如果您添加了一个对象,当您调用 toString
方法时,您将获得它的 class 和 hashCode
。因此,如果您已将对象添加到组合中,则需要覆盖 toString 方法并取回字符串(标识对象的内容)。
@Override
public String toString() {
return "some identifier";
}
public void actionPerformed(ActionEvent e) {
String[] arr = {tf1.getText(),tf2.getText(),"","","","","","","","","","","","","","","",""};
switch(cb1.getSelectedItem().toString()){
case "Hillsborough":
arr[2] = "1";
break;
case "Pinellas":
System.out.println("HIT 1");
arr[3] = "1";
break;
case "Pasco":
System.out.println("HIT 2");
arr[4] = "1";
break;
case "Hernando":
System.out.println("HIT 3");
arr[5] = "1";
break;
case "Polk":
System.out.println("HIT 3");
arr[6] = "1";
break;
case "Manatee":
System.out.println("HIT 4");
arr[7] = "1";
break;
case "Sarasota":
System.out.println("HIT 5");
arr[8] = "1";
break;
case "Other Florida":
System.out.println("HIT 6");
arr[9] = "1";
break;
case "Other State/Country":
System.out.println("HIT 7");
arr[10] = "1";
break;
default:
break;
}
switch(cb2.getSelectedItem().toString()){
case "Active Duty":
arr[11] = "1";
break;
case "Coalition Forces":
arr[12] = "1";
break;
case "Dependent":
arr[13] = "1";
break;
case "Guard/Reserve":
arr[14] = "1";
break;
case "Retired":
arr[15] = "1";
break;
case "Veteran":
arr[16] = "1";
break;
case "Civilian":
arr[17] = "1";
break;
default:
break;
}
reader.submit(2, arr);
for(int c = 0; c < arr.length;c++)
System.out.print(arr[c]);
}
When i click the confirm button it triggers this. It, depending on the selected result, will add a 1 to a specific spot in the array and the pass it on through another method. It has nothing to do with the second method, the results aren't being accessed at all. The first result in the combo box doesn't have a switch/case associated with it because those are the placeholders for the selction boxes. Any way to fix it?
And what return cbo.getSelectedItem().toString()?
使用列表而不是数组。列表是动态的,因此您可以在需要时添加元素。当您不知道数据的长度时使用数组是不可读的和不必要的。
List<String> data = new ArrayList<>(); data.add(cbo.getSelectedItem().toString());
您确定添加的是字符串而不是对象吗?如果您添加了一个对象,当您调用
toString
方法时,您将获得它的 class 和hashCode
。因此,如果您已将对象添加到组合中,则需要覆盖 toString 方法并取回字符串(标识对象的内容)。@Override public String toString() { return "some identifier"; }