0

我有一些奇怪的事情,我无法理解它为什么会发生。我搜索了包括此处在内的网络,但找不到答案。我有 3 个 ComboBox,其中 2 个接着一个。它是一个小程序转换工具。每次我选择我想在主组合框中转换的字段时,它都会用要转换的值加载另外两个。但是在每次加载后,我都会选择我想要转换的内容,这会影响 ComboBox。这是代码://这是对象//////

     MyEvent handler = new MyEvent();

    MainCombo = new JComboBox(issueArray);
    MainCombo.setBounds(120, 50, 120, 20);
    MainCombo.addActionListener(handler);
    add(MainCombo);

    Combo1 = new JComboBox(Angle);
    Combo1.setBounds(120, 90, 120, 20);
    Combo1.addActionListener(handler);
    add(Combo1);

    Combo2 = new JComboBox(Angle);
    Combo2.setBounds(320, 90, 120, 20);
    Combo2.addActionListener(handler);
    add(Combo2);

    //The Method To Change Values In The ComboBox 
     public void loadIssueParam(String Issue){
    Combo1.removeAllItems();
    Combo2.removeAllItems();
    switch (Issue) {
        case "Angle":
            {
                DefaultComboBoxModel newModel= new DefaultComboBoxModel(Angle);
                Combo1.setModel(newModel);
                Combo2.setModel(newModel);
                break;
            }
        case "Area":
            {
                DefaultComboBoxModel   newModel= new DefaultComboBoxModel(Area);
                Combo1.setModel(newModel);
                Combo2.setModel(newModel);
                break;
            }
        case "Data":
            {
                DefaultComboBoxModel  newModel= new DefaultComboBoxModel(Data);
                Combo1.setModel(newModel);
                Combo2.setModel(newModel);
                break;
            }
        case "Energy":
            {
                DefaultComboBoxModel   newModel= new DefaultComboBoxModel(Energy);
                Combo1.setModel(newModel);
                Combo2.setModel(newModel);
                break;
            }
        case "Force":
            {
                DefaultComboBoxModel      newModel= new DefaultComboBoxModel(Force);
                Combo1.setModel(newModel);
                Combo2.setModel(newModel);
                break;
            }
        case "Length":
            {
                DefaultComboBoxModel      newModel= new DefaultComboBoxModel(Length);
                Combo1.setModel(newModel);
                Combo2.setModel(newModel);
                break;
            }
        case "Mass":
            {
                DefaultComboBoxModel        newModel= new DefaultComboBoxModel(Mass);
                Combo1.setModel(newModel);
                Combo2.setModel(newModel);
                break;
            }
        case "Power":
            {
                DefaultComboBoxModel       newModel= new DefaultComboBoxModel(Power);
                Combo1.setModel(newModel);
                Combo2.setModel(newModel);
                break;
            }
        case "Pressure":
            {
                DefaultComboBoxModel  newModel= new DefaultComboBoxModel(Pressure);
                Combo1.setModel(newModel);
                Combo2.setModel(newModel);
                break;
            }
        case "Speed":
            {
                DefaultComboBoxModel  newModel= new DefaultComboBoxModel(Speed);
                Combo1.setModel(newModel);
                Combo2.setModel(newModel);
                break;
            }
        case "Temperature":
            {
                DefaultComboBoxModel  newModel= new DefaultComboBoxModel(Temperature);
                Combo1.setModel(newModel);
                Combo2.setModel(newModel);
                break;
            }
        case "Time":
            {
                DefaultComboBoxModel  newModel= new DefaultComboBoxModel(Time);
                Combo1.setModel(newModel);
                Combo2.setModel(newModel);
                break;
            }
        case "Volume":
            {
                DefaultComboBoxModel newModel= new DefaultComboBoxModel(Volume);
                Combo1.setModel(newModel);
                Combo2.setModel(newModel);
                break;
            }
    }

    /// I THINK MAYBE THIS METHOD HAVE SOMETHING TO DO WITH IT - OR I NEED SOME HOW TO
    /// CLEAR THE SELECTION.



      private class MyEvent implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent event){
        if(event.getSource()==MainCombo){
        JComboBox cb=(JComboBox)event.getSource();
        IssueBox=(String)cb.getSelectedItem();
        loadIssueParam(IssueBox);
    } 
        if(event.getSource()==Combo1){
        JComboBox cb=(JComboBox)event.getSource();
        fromBox=(String)cb.getSelectedItem();
    } 
        if(event.getSource()==Combo2){
        JComboBox cb=(JComboBox)event.getSource();
        toBox=(String)cb.getSelectedItem();
    }
    }
}

每次我进行子选择时,它都会影响两个子组合框。

感谢尼罗的帮助

4

1 回答 1

3

啊,看了你的代码,我现在明白你的问题了。您的问题是两个子组合框共享相同的模型,因此如果您更改一个子组合框中的选择,它会更改共享模型的状态,导致另一个子组合框更改其选择。解决你的问题,给他们每个人自己的模型。

IE,

switch (Issue) {
    case "Angle":
        {
            DefaultComboBoxModel newModel1 = new DefaultComboBoxModel(Angle);
            DefaultComboBoxModel newModel2 = new DefaultComboBoxModel(Angle);
            Combo1.setModel(newModel1);
            Combo2.setModel(newModel2);
            break;
        }
    // etc... 

此外,请考虑使用 Map,例如 aHashMap<String, String[]>HashMap<String, Vector<String>>。这样做并正确填写地图,并且可以将巨大的 switch 语句简化为

Combo1.setModel(new DefaultComboBoxModel(modelMap.get(Issue)));
Combo2.setModel(new DefaultComboBoxModel(modelMap.get(Issue)));

接下来我们将研究 Java 命名规则。

于 2012-10-21T00:32:06.807 回答