2017-07-25 117 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>)