2017-07-25 115 views
0

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

我想用Swing构建一个GUI,我可以注册不同种类的酒。我想要我的葡萄酒类(将有一个葡萄酒超级类和三个子类:红色,白色和玫瑰)包含一些字符串和整数(名称,年份等)和一堆对象,如国家,地区,房子和更多。

我创建了葡萄酒对象从一个JPanel现在由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!"); 
     } 
    } 

这里是我的祖国类:

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 name创建我的酒对象d Country country

希望你能理解那里有一些瑞典人。

+0

源代码中的单个空白空白行是所有* *所需的。 '{'之后或'}'之前的空行通常也是多余的。 –

回答

1

而不是添加只是国名像你现在正在做的,你将需要添加国家对象本身,这样的事情将被罚款:

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

然后在你的类“国家”,你会需要重写“toString”方法,我相信你做得很好,将代码格式化为可读性更好。

public class Country { 

    private String countryName; 

    //Constructor, getters and setters 

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

} 

,只要你想获得该国的对象你选择,你可以:

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

,让你将需要为您希望的功能的ID,姓名或其他财产实行。

希望它有帮助。

+0

这几乎可以工作,但我做错了什么,因为当我创建我的Wine对象时,我在Country上得到一个空值,我创建了一个Wine对象,但Country变量为null。所以我想,无论是当我从RoseWineForm返回“selectedCountry”还是当我将JComboBox中的国家信息丢失时。你能为我指出正确的方向吗? – SamSpj

+0

你可以发布代码,你如何创建Wine对象并指定Country值?因为组件显示值,所以当您将它放入JComboBox中时,我认为国家信息不会丢失,不是吗?请提供代码,说明如何从界面获取您的价值,以便为您提供更多帮助。 –

+0

RoseWineForm rose = new RoseWineForm(); int answer = JOptionPane.showConfirmDialog(TestVin.this,rose,“Nyttrosévin”, JOptionPane.OK_CANCEL_OPTION); 如果(回答= JOptionPane.OK_OPTION!){ \t \t \t \t \t \t回报; \t \t} else { RoseWine r = new RoseWine(rose.getName(),rose.getCountry()); allWines.add(r); allroseWines.add(r); System.out.println(r.toString()); } – SamSpj

0

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