Ich möchte bei klicken des Buttons herausfinden, welcher geklickt wurde. Hier Teile des Codes:
Zusammenfassung: Es werden im Grindbaglayout 24 Buttons erstellt. Nun ist es Aufgabe des Action Listeners herauszufinden, welcher Button geklickt wurde.
getName() liefert aber nur den Name des zuletzt initialisieren Buttons, also 24. Kann mir hier jemand weiterhelfen, verstehe auch grad nicht, wie ich die Buttons einzeln ansprechen könnte, also irgendwie mit this.button... Vielen Dank!
Java:
c.gridx = c.gridy = 0;
c.gridheight = c.gridwidth = 1;
c.weightx = c.weighty = 1.0;
c.fill = GridBagConstraints.BOTH;
int x = 0, y = 1, t = 0;
int[] tag = new int[24];
for (int i = 0; i < 24; i++) {
tag[i] = (i + 1);
}
for (int i = 0; i < 28; i++) {
if (x == 4 && y == 5) {
t++;
continue;
}
if (i % 5 == 0 && i != 0) {
y++;
x = 0;
}
c.gridx = x;
c.gridy = y;
button = new Button(tag[t] + ". Tür");
button.setName(""+tag[t]);
button.setForeground(Color.black);
button.setBackground(Color.green);
button.addActionListener(this);
grid.setConstraints(button, c);
add(button);
t++;
x++;
}
Java:
public void actionPerformed(ActionEvent e) {
System.out.println(button.getName());
}