Return 来自 JComboBox 的对象

Return object from JComboBox

我想这是一个真正的新手问题,但我无法在这里、我的 java 书中或其他地方找到任何答案。

我正在尝试使用 Swing 构建一个 GUI,我可以在其中注册不同种类的酒。我想要我的 wine-class(会有一个 wine super class 和三个 sub classes:red,white 和 rose)由一些字符串和整数(名称,年份等)和一堆对象,如国家、地区、房屋等。

我从一个 JPanel 创建了 wine 对象,现在包含一个 JTextArea 的名称和一个 JComboBox 的国家,我通过使用收集名称的 for 循环来填充我的组合框来自存储在数组列表中的对象国家/地区的变量。

这是我的桃红酒表格,其他的看起来差不多。

class RoseWineForm extends JPanel {

   private JTextField wineName = new JTextField(15);
   private JComboBox countryBox = new JComboBox();

   public RoseWineForm() {
       JPanel line1 = new JPanel();
       setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
       line1.add(new JLabel("Namn: "));
       line1.add(wineName);
       add(line1);

       JPanel line2 = new JPanel();
       line2.add(new JLabel("Ursprungsland"));
       line2.add(countryBox);
       for(Country c : listOfCountries) {
       countryBox.addItem(c.getCountryName());
       }
       add(line2);
  }

  public String getName() {
      return wineName.getText();
  }

  public Country getCountry() {
      return ;
  }}

这是将用户发送到表单的 ActionListener

class NewWineListener implements ActionListener {
 public void actionPerformed (ActionEvent a) {
     try {
         JComboBox wineColor = (JComboBox) a.getSource();
         if (wineColor.getSelectedIndex() == 0) {
             RedWineForm red = new RedWineForm();
             int answer = JOptionPane.showConfirmDialog(TestVin.this, red, "Nytt rött vin",
                JOptionPane.OK_CANCEL_OPTION);
         } else if (wineColor.getSelectedIndex() == 1) {
             WhiteWineForm white = new WhiteWineForm();
             int answer = JOptionPane.showConfirmDialog(TestVin.this, white, "Nytt vitt vin",
                JOptionPane.OK_CANCEL_OPTION);
         } else {
             RoseWineForm rose = new RoseWineForm();
             int answer = JOptionPane.showConfirmDialog(TestVin.this, rose, "Nytt rosé vin",
                JOptionPane.OK_CANCEL_OPTION);
         }
     } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(TestVin.this, "Fel inmatning!");
        }
    }

这是我的国家class:

public class Country {

private String countryName;

public Country(String countryName) {
    this.countryName = countryName;
}

public String getCountryName() {
    return countryName;
}

public void setCountryName(String newCountryName) {
    if ( newCountryName == null || newCountryName.trim().isEmpty()) {
       System.out.println("You have to set a name.");       
    }  else {
    countryName = newCountryName;
    }} 
    public String toString() {
    return countryName;         

}
}

我的问题是:当我 select 我的组合框中的国家/地区名称时,我如何 return 对象而不仅仅是 String 调用 countryName 以便我可以使用变量 String nameCountry country?

创建我的 wine 对象

希望你能理解里面有一些瑞典语。

我相信您正在寻找 DefaultComboBoxModel class 因为这允许您动态分配对象给它。然后在国家/地区,您需要 @Override toString() 函数到 return countryName 这样当 DefaultComboBoxModel 对象推送到组合框时它会显示名称但是如果有道理,它将 return 反对。为了将您创建的模型设置为 JPanel,您可以使用 countryBox.setModel(<name of DefaultComboBoxModel>).

与像现在这样只添加国家名称不同,您需要添加国家对象本身,这样就可以了:

public RoseWineForm() {
       JPanel line1 = new JPanel();
       setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
       line1.add(new JLabel("Namn: "));
       line1.add(wineName);
       add(line1);

       JPanel line2 = new JPanel();
       line2.add(new JLabel("Ursprungsland"));
       line2.add(countryBox);
       for(Country c : listOfCountries) {
           //This does the trick
           countryBox.addItem(c);
       }
       add(line2);
  }

然后在你的 class "Country" 中你需要覆盖 "toString" 方法,我相信你做得很好,格式化你的代码是个好主意它更具可读性。

public class Country {

    private String countryName;

    //Constructor, getters and setters

    @Override
    public String toString() {
        return this.countryName;
    }

}

无论何时你想要获取你选择的国家对象,你可以:

Country selectedCountry = (Country) countryBox.getSelectedItem();

并获取 ID、名称或任何其他 属性 您需要实现的功能。

希望对您有所帮助。