Ampelschaltung

DerAtze

Mitglied
Hallo,

ich habe in der Schule den unten stehenden Quelltext für eine Ampelschaltung bekommen, bei dem beim Klick auf den Button die neue Ampelphase mit Fußgängerampel und normaler Ampel gestartet werden sollte. Ich weiß allerdings nicht, wie ich die nächste Ampelphase einfügen muss. Wäre nett wenn mir jemand hierbei behilflich sein könnte.


Java:
// Anfang Methoden
  public static void main(String[] args) {
    new Ampelschaltung("Ampelschaltung");
  } // end of main
  public void jButton1_ActionPerformed(ActionEvent ae) {
    if (phaseAmpel == 1) {
      jPanel1.setBackground(Color.RED);
      jPanel2.setBackground(Color.WHITE);
      jPanel3.setBackground(Color.WHITE);
      jPanel4.setBackground(Color.WHITE);
      jPanel5.setBackground(Color.GREEN);
    
    }

  }// end of if
  
  
} // end of jButton1_ActionPerformed 

   // Ende Methoden
 

Joose

Top Contributor
Bitte Code immer in Code-Tags packen [java] ... dein code .. [/java]

Ich nehme an am Ende der "actionPerformed" Methode sollte der Wert von "phaseAmpel" um 1 erhöht werden.
Und für die anderen Phasen schreibst du einfach ebenfalls if Bedingungen hin (natürlich mit der richtigen Farbreihenfolge).
Ein Beispiel für diese if-Bedingung hast du ja schon vorgegeben.
 

DerAtze

Mitglied
Habe es jetzt so aufgeschrieben und es funktioniert trotzdem nicht

Java:
  // Anfang Methoden
  public static void main(String[] args) {
    new Ampelschaltung("Ampelschaltung");
  } // end of main
  public void jButton1_ActionPerformed(ActionEvent e) {
    {
      if (phaseAmpel == 1) {
        jPanel1.setBackground(Color.RED);
        jPanel2.setBackground(Color.WHITE);
        jPanel3.setBackground(Color.WHITE);
        jPanel4.setBackground(Color.WHITE);
        jPanel5.setBackground(Color.GREEN);  
       
        jPanel1.setBackground(Color.RED);
        jPanel2.setBackground(Color.YELLOW);
        jPanel3.setBackground(Color.WHITE);
        jPanel4.setBackground(Color.RED);
        jPanel5.setBackground(Color.WHITE); 
       
        jPanel1.setBackground(Color.WHITE);
        jPanel2.setBackground(Color.WHITE);
        jPanel3.setBackground(Color.GREEN);
        jPanel4.setBackground(Color.WHITE);
        jPanel5.setBackground(Color.WHITE); 
       
       
      } 
    } 
  }// end of if
 

JStein52

Top Contributor
Java:
public void jButton1_ActionPerformed(ActionEvent e) {
    {
      if (phaseAmpel == 1) {
        jPanel1.setBackground(Color.RED);
        jPanel2.setBackground(Color.WHITE);
        jPanel3.setBackground(Color.WHITE);
        jPanel4.setBackground(Color.WHITE);
        jPanel5.setBackground(Color.GREEN);
        phaseAmpel = 2;
      } else if (phaseAmpel == 2) {
        jPanel1.setBackground(Color.RED);
        jPanel2.setBackground(Color.YELLOW);
        jPanel3.setBackground(Color.WHITE);
        jPanel4.setBackground(Color.RED);
        jPanel5.setBackground(Color.WHITE);
        phaseAmpel = 3;
      } else if (phaseAmpel == 3) {
        jPanel1.setBackground(Color.WHITE);
        jPanel2.setBackground(Color.WHITE);
        jPanel3.setBackground(Color.GREEN);
        jPanel4.setBackground(Color.WHITE);
        jPanel5.setBackground(Color.WHITE);
        phaseAmpel = 1;   // wieder von vorne
      }
    }
  }// end of if

ich weiss aber nicht ob das alle Phasen sind bzw. was diese Panels alle darstellen. Aber das Prinzip sollte wohl klar sein oder ? Farben setzen und die aktuelle Phase fortschalten.

(und keine Gewähr dafür dass die geschweiften Klammern am Ende passen :):))
 
Zuletzt bearbeitet von einem Moderator:

Neue Themen


Oben