无法从组合框

时间:2016-06-26 23:03:35

标签: java swing combobox

我已实施ActionListener,以便我的船名单根据国家和船舶类型而变化,但问题是我无法选择任何船只。

关于我可能出错的任何想法?

private class ShipNameListener implements ActionListener
{   
    public ShipNameListener()
    {
        view.setShipNameListener(this);
    }   

    @Override
    public void actionPerformed(ActionEvent arg0) 
    {           
        if (view.getNationComboBox().getSelectedItem() == "USA")
        {
            if (view.getShipTypeComboBox().getSelectedItem() == "Battleship")
            {
                view.setShipList(lists.getUSABattleships());
            }
            else if (view.getShipTypeComboBox().getSelectedItem() == "CV")
            {
                view.setShipList(lists.getUSACVs());
            }
            else if (view.getShipTypeComboBox().getSelectedItem() == "Destroyer")
            {
                view.setShipList(lists.getUSADestroyers());
            }
            else if (view.getShipTypeComboBox().getSelectedItem() == "Cruiser")
            {
                view.setShipList(lists.getUSACruisers());
            }
        }           
    }       
}

上面是Controller类中的ActionListener

public void setShipNameListener(ActionListener al) 
{
    comboBoxNation.addActionListener(al);
    comboBoxShipType.addActionListener(al);
    comboBoxShipName.addActionListener(al);     
}

上面是View类中的setter。

Can't select other items from combobox

1 个答案:

答案 0 :(得分:2)

不要使用==进行对象比较。您只能使用==进行原始比较。

对于对象,请使用equals(...)方法。