JComboBox没有返回预期的结果

时间:2015-08-14 14:02:38

标签: 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]);
    }
  

当我点击确认按钮时,它会触发此操作。它取决于所选的结果,将1添加到数组中的特定点,并通过另一个方法传递它。它与第二种方法无关,结果根本没有被访问。组合框中的第一个结果没有与之关联的开关/案例,因为它们是选择框的占位符。有什么办法解决吗?

1 个答案:

答案 0 :(得分:1)

  

什么返回cbo.getSelectedItem()。toString()?

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

    toString
  2. 您确定已添加字符串而非对象吗?如果您添加了对象,则在致电hashCode方法时,您将获得该课程,并@Override public String toString() { return "some identifier"; } 获取该课程。因此,如果您已经为组合添加了一个Object,则需要覆盖toString方法并返回一个String(标识该Object的东西)。

    drawLabelsEnabled