Hallo an alle!
Wir sollen als Aufgabe eine Swing Anwendung mit 3 Buttons (zum ändern der Farbe) erstellen. Leider bringt mir der Compiler immer noch zwei Fehler, hab keinen blassen schimmer ob ich den ActionListener falsch implementiert habe oder ob da irgendwas anderes nicht stimmt. Wäre nett, wenn mir jemand einen Tip geben könnte. Danke schon mal. Gruß, dasie
Wir sollen als Aufgabe eine Swing Anwendung mit 3 Buttons (zum ändern der Farbe) erstellen. Leider bringt mir der Compiler immer noch zwei Fehler, hab keinen blassen schimmer ob ich den ActionListener falsch implementiert habe oder ob da irgendwas anderes nicht stimmt. Wäre nett, wenn mir jemand einen Tip geben könnte. Danke schon mal. Gruß, dasie
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonBox implements ActionListener {
public static void main (String args[]) {
JFrame bb = new JFrame("ButtonBox");
JPanel pa = new JPanel( new GridLayout (3, 1));
JButton g = new JButton("faerbe Buttons gelb");
g.addActionListener(this);
JButton r = new JButton("faerbe Buttons rot");
r.addActionListener(this);
JButton b = new JButton("faerbe Buttons blau");
b.addActionListener(this);
pa.add(g);
pa.add(r);
pa.add(b);
bb.setForeground(Color.black);
b.setForeground(Color.black);
b.setBackground(Color.yellow);
g.setForeground(Color.black);
g.setBackground(Color.yellow);
r.setForeground(Color.black);
r.setBackground(Color.yellow);
bb.getContentPane().add(pa);
bb.setVisible(true);
bb.pack();
public void actionPerformed(ActionEvent e){
if(e.getActionCommand()== "faerbe Buttons gelb"){
g.setBackground(Color.yellow);
r.setBackground(Color.yellow);
b.setBackground(Color.yellow);
}
if(e.getActionCommand() == "faerbe Buttons rot"){
g.setBackground(Color.red);
r.setBackground(Color.red);
b.setBackground(Color.red);
}
if(e.getActionCommand()== "faerbe Buttons blau"){
g.setBackground(Color.blue);
r.setBackground(Color.blue);
b.setBackground(Color.blue);
}
}
}
}