Pausen

CptK

Bekanntes Mitglied
Hallo,
ich muss für die Schule eine Ampelschaltung programmieren und ich habe dort ein kleines Problem: Standartgemäß ist die Ampel rot, betätigt man den Button, soll sie kurz gelb werden und dann grün, in meinem code wird das Gelb-werden aber einfach übersprungen, ich weiß leider nicht wieso. Ich schicke mal den ganzen Code, problematisch ist vermutlich aber nur der code am Ende.

Java:
package Ampel;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.*;

public class Ampel extends JFrame {
   
    static Ampel frame;
    static JPanel contentPane;
    private JTextField textRed;
    private JTextField textYellow;
    private JTextField textGreen;
    private JButton btnRun;
    static boolean red = true, green = false, redtogreen = false, greentored = false;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    frame = new Ampel();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Ampel() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 300, 530);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
       
        JLabel lblAmpel = new JLabel("Ampel");
        lblAmpel.setFont(new Font("Impact", Font.BOLD, 44));
        lblAmpel.setBounds(63, 11, 151, 63);
        contentPane.add(lblAmpel);
       
        textRed = new JTextField();
        textRed.setEditable(false);
        textRed.setBounds(10, 85, 264, 77);
        textRed.setBackground(Color.LIGHT_GRAY);
        contentPane.add(textRed);
        textRed.setColumns(10);
       
        textYellow = new JTextField();
        textYellow.setEditable(false);
        textYellow.setColumns(10);
        textYellow.setBounds(10, 184, 264, 77);
        textYellow.setBackground(Color.LIGHT_GRAY);
        contentPane.add(textYellow);
       
        textGreen = new JTextField();
        textGreen.setEditable(false);
        textGreen.setColumns(10);
        textGreen.setBounds(10, 279, 264, 77);
        textGreen.setBackground(Color.LIGHT_GRAY);
        contentPane.add(textGreen);
       
        if(red == true) {
            textRed.setBackground(Color.RED);
        }
       
        btnRun = new JButton("Umschalten");
        btnRun.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if(red == true) {
                    red = false;
                    redtogreen = true;
                } else if(green == true) {
                    green = false;
                    greentored = true;
                }
               
                if(redtogreen == true) {
                    try {
                        Thread.sleep(500);
                        textRed.setBackground(Color.LIGHT_GRAY);
                        textYellow.setBackground(Color.YELLOW);
                        Thread.sleep(500);   
                        textYellow.setBackground(Color.LIGHT_GRAY);
                        textGreen.setBackground(Color.GREEN);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                   
                   
                }
            }
        });
        btnRun.setBounds(10, 430, 264, 50);
        contentPane.add(btnRun);
    }
}

Vielen Dank für die Hilfe!
 

MoxxiManagarm

Top Contributor
Ja das meine ich. In dem Link ist es gut erklärt. Während sleep werden alle repaint Anfragen im EDT gequeued und später durchgeführt. javax.swing.Timer ist EDT freundlich und meines Erachtens und wenn man das einmal verstanden hat auch leslicher. Dein Code ist dann actionbezogen.
 

Neue Themen


Oben