Ich will ein Button durch ein Bild ersetzten, wenn auf den Button geklickt wird. Das ActionPerformed Event sieht so aus:
Und das Fenster an sich:
Jedoch klappt die aktuelle Methode noch nicht. Zudem würde ich das ganze gerne mit Awt machen und nocht mit Swing(JFrame); Wäre nett wenn mir jemand weiterhelfen könnte, ich komm grad einfach nicht selber drauf wo der Fehler liegt.
Java:
case 6: ((Button) e.getSource()).setVisible(false);
ImageIcon icon = new ImageIcon("nikolaus.jpg");
JLabel bild = new JLabel(icon);
grid.setConstraints(bild,g);
add(bild);
bild.setVisible(true);
break;
Java:
GridBagLayout grid = new GridBagLayout();
GridBagConstraints g = new GridBagConstraints();
Button button;
private int year = 2011;//Calendar.getInstance().get(Calendar.YEAR);
public static void main(String[] args) {
new Kalender();
}
public Kalender() {
setTitle("Weihnachtskalender " + year);
setBackground(Color.RED);
setSize(400, 400);
setLayout(grid);
addWindowListener(this);
g.gridx = g.gridy = 0;
g.gridheight = g.gridwidth = 1;
g.weightx = g.weighty = 1.0;
g.fill = GridBagConstraints.BOTH;
int x = 0, y = 1, t = 0;
int[] tag = initialisiere();
// 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;
}
g.gridx = x;
g.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, g);
add(button);
t++;
x++;
}
g.gridx = 0;
g.gridy = 0;
g.gridheight = 1;
g.gridwidth = 0;
// button = new Button("Weihnachtskalender " +
// getYear()+" <Klick für Infos>");
// button.setForeground(Color.black);
// button.setFont(new Font("sansserif",Font.BOLD,16));
// button.setBackground(Color.green);
Label label = new Label("Weihnachtskalender " + year);
label.setFont(new Font("sansserif", Font.BOLD, 16));
label.setForeground(Color.green);
grid.setConstraints(label, g);
add(label);
setVisible(true);