Ich denke, das ist eine echte Newbie Frage, aber ich kann keine Antworten hier finden, in meinem Java-Buch oder anderswo.Rückgabe Objekt von JComboBox
Ich versuche eine GUI mit Swing zu erstellen, wo ich verschiedene Arten von Wein registrieren kann. Ich möchte meine Weinklasse (es gibt eine Wein-Super-Klasse und drei Unterklassen: Rot, Weiß und Rose), die sowohl aus Strings und Ganzzahlen bestehen (Name, Jahr usw.) als auch aus einer Reihe von Objekten wie Country, District, Haus und mehr.
Ich schaffe den Wein Objekt aus einem JPanel jetzt eines JTextArea
für den Namen aus und JComboBox
für das Land, bevölkern ich meine Combo-Box mit einer for-Schleife, die den Namen Variable aus dem Objekt Land sammelt, die in einem gespeichert wird Anordnungsliste.
Hier ist meine Form für Rosé, die anderen sehen ungefähr gleich aus.
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 ;
}}
Hier ist die ActionListener
, die den Benutzer auf die Formen
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!");
}
}
Hier ist mein Land Klasse sendet:
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;
}
}
Meine Frage ist: Wenn ich Namen wählen Sie das Land in meinem Kombinationsfeld Wie gebe ich das Objekt zurück und nicht nur das String
namens countryName
, damit ich mein Weinobjekt mit den Variablen String name
anlegen kann d Country country
?
Hoffe, Sie können verstehen, dass es einige schwedisch gibt.
Eine einzelne leere Zeile Leerraum im Quellcode ist alles, was * jemals * benötigt wird. Leere Zeilen nach '{' oder vor '}' sind normalerweise redundant. –