java - JComboBox 未返回预期结果

标签 java jcombobox

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()?

  1. 使用列表而不是数组。列表是动态的,因此您可以在需要时添加元素。当您不知道数据长度时使用数组是不可读且不必要的。

    List<String> data = new ArrayList<>();
    data.add(cbo.getSelectedItem().toString());
    
  2. 您确定添加了字符串而不是对象吗?如果您添加了一个对象,当您调用 toString 方法时,您将获得该类和它的 hashCode。因此,如果您已将一个对象添加到组合中,则需要重写 toString 方法并返回一个字符串(标识该对象的内容)。

    @Override
    public String toString() {
        return "some identifier";
    }
    

关于java - JComboBox 未返回预期结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32011984/

相关文章:

java - 如何从java访问在php中创建的memcache项

java - JComboBox 和 Action、新模型等

java - JComboBox 每个项目的颜色不同失败

Java ComboBox 什么控件控制列表的显示位置?

Java:保存要在循环中计算的用户输入

java - 当我点击 map 上 3 个制造商中的 1 个时如何从 Firebase 获取数据

java - ChoiceBox onShown\onHidden 方法对 JavaFX 不起作用

java - 空指针异常(救命!)

java - 从 jcombobox 获取数据并将其转换为 float

Java:使 jcombobox 的一项不可选择(如子标题)并编辑该项目的字体