Hallo,
ich habe ein Problem in Java, und zwar soll ich eine Ampelschaltung erstellen. Die erste Seite habe ich schon abgeschlossen, jetzt soll ich als nächstes noch rechts, links und gegenüber jeweils eine weitere Ampel und Fußgängerampel nach dem gleichen Muster wie bisher hinpacken, so dass diese wie an einer Kreuzung nacheinander geschaltet werden. Wäre nett wenn mir jemand dabei helfen kann und mir das hier einfach mal vor macht.
Hier mein bisheriger Quelltext:
Danke im Vorraus!
ich habe ein Problem in Java, und zwar soll ich eine Ampelschaltung erstellen. Die erste Seite habe ich schon abgeschlossen, jetzt soll ich als nächstes noch rechts, links und gegenüber jeweils eine weitere Ampel und Fußgängerampel nach dem gleichen Muster wie bisher hinpacken, so dass diese wie an einer Kreuzung nacheinander geschaltet werden. Wäre nett wenn mir jemand dabei helfen kann und mir das hier einfach mal vor macht.
Hier mein bisheriger Quelltext:
Java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/**
*
* Beschreibung
*
* @version 1.0 vom 23.01.2016
* @author
*/
public class Ampelschaltung extends JFrame {
// Anfang Attribute
private JPanel jPanel1 = new JPanel(null, true);
private JPanel jPanel2 = new JPanel(null, true);
private JPanel jPanel3 = new JPanel(null, true);
private JPanel jPanel4 = new JPanel(null, true);
private JPanel jPanel5 = new JPanel(null, true);
private JPanel jPanel6 = new JPanel(null, true);
private JPanel jPanel7 = new JPanel(null, true);
private JPanel jPanel8 = new JPanel(null, true);
private JPanel jPanel9 = new JPanel(null, true);
private JButton jButton1 = new JButton();
// Attribut: Phase der Ampelschaltung
private int phaseAmpel = 1;
// Ende Attribute
public Ampelschaltung(String title) {
// Frame-Initialisierung
super(title);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
int frameWidth = 352;
int frameHeight = 405;
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);
setResizable(false);
Container cp = getContentPane();
cp.setLayout(null);
// Anfang Komponenten
jPanel1.setBounds(64, 40, 60, 60);
jPanel1.setOpaque(true);
jPanel1.setBackground(Color.WHITE);
cp.add(jPanel1);
jPanel2.setBounds(64, 120, 60, 60);
jPanel2.setOpaque(true);
jPanel2.setBackground(Color.WHITE);
cp.add(jPanel2);
jPanel3.setBounds(64, 200, 60, 60);
jPanel3.setOpaque(true);
jPanel3.setBackground(Color.WHITE);
cp.add(jPanel3);
jPanel4.setBounds(234, 140, 40, 40);
jPanel4.setOpaque(true);
jPanel4.setBackground(Color.WHITE);
cp.add(jPanel4);
jPanel5.setBounds(234, 220, 40, 40);
jPanel5.setOpaque(true);
jPanel5.setBackground(Color.WHITE);
cp.add(jPanel5);
jPanel6.setBounds(249, 180, 10, 200);
jPanel6.setOpaque(true);
jPanel6.setBackground(Color.WHITE);
cp.add(jPanel6);
jPanel7.setBounds(84, 100, 20, 300);
jPanel7.setOpaque(true);
jPanel7.setBackground(Color.WHITE);
cp.add(jPanel7);
jButton1.setBounds(128, 296, 100, 48);
jButton1.setText("Nächste Phase");
jButton1.setMargin(new Insets(1, 2, 3, 4));
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton1_ActionPerformed(evt);
}
});
cp.add(jButton1);
// Ende Komponenten
setVisible(true);
} // end of public Ampelschaltung
// Anfang Methoden
public static void main(String[] args) {
new Ampelschaltung("Ampelschaltung");
} // end of main
public void jButton1_ActionPerformed(ActionEvent evt) {
{
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; //nächste Phase
} else if (phaseAmpel == 2) {
jPanel1.setBackground(Color.RED);
jPanel2.setBackground(Color.WHITE);
jPanel3.setBackground(Color.WHITE);
jPanel4.setBackground(Color.RED);
jPanel5.setBackground(Color.WHITE);
phaseAmpel = 3; //nächste Phase
} else if (phaseAmpel == 3) {
jPanel1.setBackground(Color.RED);
jPanel2.setBackground(Color.YELLOW);
jPanel3.setBackground(Color.WHITE);
jPanel4.setBackground(Color.RED);
jPanel5.setBackground(Color.WHITE);
phaseAmpel = 4; //nächste Phase
} else if (phaseAmpel == 4) {
jPanel1.setBackground(Color.WHITE);
jPanel2.setBackground(Color.WHITE);
jPanel3.setBackground(Color.GREEN);
jPanel4.setBackground(Color.RED);
jPanel5.setBackground(Color.WHITE);
phaseAmpel = 5; //nächste Phase
} else if (phaseAmpel == 5) {
jPanel1.setBackground(Color.WHITE);
jPanel2.setBackground(Color.YELLOW);
jPanel3.setBackground(Color.WHITE);
jPanel4.setBackground(Color.RED);
jPanel5.setBackground(Color.WHITE);
phaseAmpel = 6; //nächste Phase
} else if (phaseAmpel == 6) {
jPanel1.setBackground(Color.RED);
jPanel2.setBackground(Color.WHITE);
jPanel3.setBackground(Color.WHITE);
jPanel4.setBackground(Color.RED);
jPanel5.setBackground(Color.WHITE);
phaseAmpel = 1; //von vorne
}// end of if
} // end of jButton1_ActionPerformed
} // Ende Methoden
} // end of class Ampelschaltung
Zuletzt bearbeitet: