Moin moin,
ich komme einfach nicht drauf. Warum wird mir bei Änderung in der Combobox nichts ausgegeben?
Rein „technisch“ sollte es doch funktionieren.
package grafiktest;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.Button;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
public class GrafikTest extends JApplet implements Runnable
{
private MyContentPane myContent = null;
@Override
public void init()
{
myContent = new MyContentPane();
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(myContent, BorderLayout.NORTH);
this.start();
}
public void run()
{
boolean running = true;
while(running)
{
super.repaint();
try
{
Thread.sleep(200);
}
catch(InterruptedException error)
{
running = false;
}
}
}
class MyContentPane extends JPanel
{
private Button abschicken = null;
private Button loeschen = null;
private JLabel nameLabel = null;
private JLabel vornameLabel = null;
private JLabel emailLabel = null;
private JCheckBox cb1 = null;
private JCheckBox cb2 = null;
private JCheckBox cb3 = null;
private JCheckBox cb4 = null;
private JTextField tf1 = null;
private JTextField tf2 = null;
private JTextField tf3 = null;
private JComboBox ch1 = null;
//private static final long serialVersionUID = 1L;
private JComboBox select;
public MyContentPane()
{
init();
addElements();
}
public MyContentPane(String name)
{
this.setName(name);
init();
addElements();
}
private void init()
{
abschicken = new Button(„Abschicken“);
abschicken.setActionCommand(„abschicken“);
abschicken.addActionListener(new ActionAdapter(this));
loeschen = new Button(„löschen“);
loeschen.setActionCommand(„löschen“);
loeschen.addActionListener(new ActionAdapter(this));
nameLabel = new JLabel(„Name:“);
vornameLabel = new JLabel(„Vorname:“);
emailLabel = new JLabel(„E-Mail:“);
cb1 = new JCheckBox(„Freunde“, false);
cb2 = new JCheckBox(„Werbung“, true);
cb3 = new JCheckBox(„Suchmaschine“, false);
cb4 = new JCheckBox(„Suchmaschien“, false);
tf1 = new JTextField(15);
tf2 = new JTextField(15);
tf3 = new JTextField(15);
ch1 = new JComboBox ();
ch1.addItem(„Deutsch“);
ch1.addItem(„Französisch“);
ch1.addItem(„Spanisch“);
ch1.addItem(„Italienisch“);
ch1.addItem(„Dänisch“);
}
private void addElements()
{
GridBagLayout gridBagLayout = new GridBagLayout();
GridBagConstraints constr = new GridBagConstraints();
this.setLayout(new GridBagLayout());
constr.ipadx = 0;
constr.ipady = 0;
constr.gridx = 10;
constr.gridy = 10;
constr.anchor = GridBagConstraints.NORTHWEST;
constr.gridx = 13;
constr.gridy = 24;
this.add(abschicken, constr);
constr.gridx = 13;
constr.gridy = 25;
this.add(loeschen, constr);
constr.gridx = 12;
constr.gridy = 20;
this.add(nameLabel, constr);
constr.gridy = 21;
this.add(vornameLabel, constr);
constr.gridy = 22;
this.add(emailLabel, constr);
constr.gridx = 13;
constr.gridy = 20;
this.add(tf1, constr);
constr.gridy = 21;
this.add(tf2, constr);
constr.gridy = 22;
this.add(tf3, constr);
constr.gridx = 13;
constr.gridy = 9;
this.add(cb1, constr);
constr.gridy = 10;
this.add(cb2, constr);
constr.gridy = 11;
this.add(cb3, constr);
constr.gridy = 12;
this.add(cb4, constr);
constr.gridy = 26;
constr.gridx = 13;
this.add(ch1, constr);
}
public Button getAbschicken()
{
return abschicken;
}
public void setAbschicken(Button abschicken)
{
this.abschicken = abschicken;
}
public Button getLoeschen()
{
return loeschen;
}
public void setLoeschen(Button loeschen)
{
this.loeschen = loeschen;
}
public JComboBox getSelect()
{
return select;
}
public void setSelect(JComboBox select)
{
this.select = select;
}
}
class ActionAdapter implements ActionListener
{
private MyContentPane myContent = null;
public ActionAdapter(JPanel myContentPane)
{
this.myContent = (MyContentPane)myContentPane;
}
ItemListener ItemSelect = new ItemListener()
{
JComboBox ch1 = new JComboBox();
public void itemStateChanged(ItemEvent e)
{
String nationalitaet = null;
if (ch1.getSelectedItem().equals(„Deutsch“))
nationalitaet=„Deutsch“;
if (ch1.getSelectedItem().equals(„Französisch“))
nationalitaet=„Franzoesisch“;
if (ch1.getSelectedItem().equals(„Spanisch“))
nationalitaet=„Spanisch“;
if (ch1.getSelectedItem().equals(„Italienisch“))
nationalitaet=„Italienisch“;
if (ch1.getSelectedItem().equals(„Dänisch“))
nationalitaet=„Daenisch“;
System.out.println(„Die Nationalitaet ist „+nationalitaet+“.“);
}
};
public void actionPerformed(ActionEvent actionEvent)
{
if(actionEvent.getActionCommand().equals(„abschicken“))
{
System.out.println(„Der Abschicken-Button wurde gedrückt“);
}
if(actionEvent.getActionCommand().equals(„löschen“))
{
System.out.println(„Der Löschen-Button wurde gedrückt“);
}
}
}
}
Habt ihr ne Idee?
Gruß Rolf