Hi Leute,
ich soll 'ne Ampel mit 'nem Frame programmieren, die nur 1 Button hat. Mein Problem ist, dass ich mit den Bedingungen für den if-Befehl nicht zurecht komme:
ich soll 'ne Ampel mit 'nem Frame programmieren, die nur 1 Button hat. Mein Problem ist, dass ich mit den Bedingungen für den if-Befehl nicht zurecht komme:
Java:
public class Ampel extends JFrame {
// Anfang Attribute
private JPanel jPanel1 = new JPanel(null);
private JPanel jPanel2 = new JPanel(null);
private JPanel jPanel3 = new JPanel(null);
private JButton jButton1 = new JButton();
// Ende Attribute
public Ampel(String title) {
// Frame-Initialisierung
super(title);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
int frameWidth = 298;
int frameHeight = 556;
setSize(frameWidth, frameHeight);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width - getSize().width) / 2;
int y = (d.height - getSize().height) / 2;
setLocation(x, y);
Container cp = getContentPane();
cp.setLayout(null);
// Anfang Komponenten
jPanel1.setBounds(69, 87, 99, 100);
cp.add(jPanel1);
jPanel2.setBounds(69, 192, 99, 99);
cp.add(jPanel2);
jPanel3.setBounds(69, 293, 100, 100);
cp.add(jPanel3);
jButton1.setBounds(69, 418, 99, 38);
jButton1.setText("jButton1");
jButton1.setMargin(new Insets(2, 2, 2, 2));
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton1_ActionPerformed(evt);
}
});
jPanel1.setBackground(Color.GRAY);
jPanel2.setBackground(Color.GRAY);
jPanel3.setBackground(Color.GRAY);
cp.add(jButton1);
// Ende Komponenten
setResizable(false);
setVisible(true);
}
// Anfang Methoden
public void jButton1_ActionPerformed(ActionEvent evt) {
if(Color.gray.equals(jPanel1) && Color.gray.equals(jPanel2) && Color.gray.equals(jPanel3)){
jPanel1.setBackground(Color.gray);
jPanel2.setBackground(Color.yellow);
jPanel3.setBackground(Color.gray);
}else if(Color.gray.equals(jPanel1) && Color.yellow.equals(jPanel2) && Color.gray.equals(jPanel3)){
jPanel1.setBackground(Color.red);
jPanel2.setBackground(Color.gray);
jPanel3.setBackground(Color.gray);
}else if(Color.red.equals(jPanel1) && Color.gray.equals(jPanel2) && Color.gray.equals(jPanel3)){
jPanel1.setBackground(Color.red);
jPanel2.setBackground(Color.yellow);
jPanel3.setBackground(Color.gray);
}else if(Color.red.equals(jPanel1) && Color.yellow.equals(jPanel2) && Color.gray.equals(jPanel3)){
jPanel1.setBackground(Color.gray);
jPanel2.setBackground(Color.gray);
jPanel3.setBackground(Color.green);
}else if(Color.gray.equals(jPanel1) && Color.gray.equals(jPanel2) && Color.green.equals(jPanel3)){
jPanel1.setBackground(Color.gray);
jPanel2.setBackground(Color.yellow);
jPanel3.setBackground(Color.gray);
}// TODO hier Quelltext einfügen
}
// Ende Methoden
public static void main(String[] args) {
new Ampel("Ampel");
}
}