有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

来自JComboBox的java返回对象

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

我正在尝试使用Swing构建一个GUI,在这里我可以注册不同种类的葡萄酒。我希望我的wine类(将有一个wine super类和三个子类:red、white和rose)由一些字符串和整数(名称、年份等)以及一系列对象组成,比如Country、District、House等等

我从一个JPanel创建wine对象,这个JPanel现在由一个JTextArea代表名称和一个JComboBox代表国家组成。我使用一个for循环来填充我的组合框,该循环从存储在arraylist中的对象国家收集名称变量

这是我的玫瑰酒表格,其他的看起来差不多

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!");
        }
    }

这是我的乡村课:

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;         

}
}

我的问题是:当我在组合框中选择国家名称时,我如何返回对象,而不仅仅是名为countryNameString,这样我就可以用变量String nameCountry country创建葡萄酒对象

希望你能理解里面有瑞典人


共 (2) 个答案

  1. # 1 楼答案

    我相信您正在寻找DefaultComboBoxModel类,因为它允许您动态地将对象分配给它。然后在country中,您需要@Override函数返回countryName,这样当DefaultComboBoxModel对象推到组合框时,它将显示名称,但如果有意义,它将返回对象。为了将您创建的模型设置为JPanel,您可以使用countryBox.setModel(<name of DefaultComboBoxModel>)

  2. # 2 楼答案

    您不需要像现在这样只添加国家名称,而是需要添加国家对象本身,类似这样的内容就可以了:

    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);
      }
    

    然后在你的类“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、名称或任何其他属性

    希望有帮助