线程:交通灯

要求:

* 1. 开始执行时只有红灯亮
 * 2. 3秒后,红灯暗掉,绿灯亮,持续5秒
 * 3. 然后绿灯暗掉,只有黄灯亮起,持续2秒
 * 4. 然后,黄灯应该变为绿灯
 * 5. 以上程序一直继续
 * 6. 还需要显示每种信号灯剩余的时间

代码:

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * @author: Annie
 * @date:2016年6月14日
 * @description:交通灯
 * 1. 开始执行时只有红灯亮
 * 2. 3秒后,红灯暗掉,绿灯亮,持续5秒
 * 3. 然后绿灯暗掉,只有黄灯亮起,持续2秒
 * 4. 然后,黄灯应该变为绿灯
 * 5. 以上程序一直继续
 * 6. 还需要显示每种信号灯剩余的时间
 * 
 */
public class SingleDemo implements Runnable{

    JFrame jf;
    JPanel jpRed,jpYellow,jpGreen;
    JLabel jltime,jlshow;

    public SingleDemo() {

        jpRed = new JPanel();
        jpRed.setBounds(110, 10, 80, 50);
        jpRed.setBackground(Color.GRAY);

        jpYellow = new JPanel();
        jpYellow.setBounds(110, 80, 80, 50);
        jpYellow.setBackground(Color.GRAY);

        jpGreen = new JPanel();
        jpGreen.setBounds(110, 150, 80, 50);
        jpGreen.setBackground(Color.GRAY);

        jltime = new JLabel("Time remaining:");
        jltime.setBounds(70, 230,100, 20);
        jltime.setForeground(Color.BLUE);

        jlshow = new JLabel();
        jlshow.setBounds(180, 230,100, 20);
        jlshow.setForeground(Color.BLUE);

        jf = new JFrame("交通灯");
        jf.setSize(300, 300);
        jf.setLayout(null);
        jf.add(jpRed);
        jf.add(jpYellow);
        jf.add(jpGreen);
        jf.add(jltime);
        jf.add(jlshow);

        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void run() {
        try {
            while(true){
                jpRed.setBackground(Color.RED);
                jpYellow.setBackground(Color.GRAY);
                jpGreen.setBackground(Color.GRAY);
                for (int i = 3; i >=0; i--) {
                    jlshow.setText(Integer.toString(i)+"---stop----");
                    Thread.sleep(1000);
                }
                
                
                jpGreen.setBackground(Color.GREEN);
                jpYellow.setBackground(Color.GRAY);
                jpRed.setBackground(Color.GRAY);
                for (int i = 5; i >=0; i--) {
                    jlshow.setText(Integer.toString(i)+"---stop----");
                    Thread.sleep(1000);
                }
                
                jpYellow.setBackground(Color.YELLOW);
                jpGreen.setBackground(Color.GRAY);
                jpRed.setBackground(Color.GRAY);
                for (int i = 2; i >=0; i--) {
                    jlshow.setText(Integer.toString(i)+"---stop----");
                    Thread.sleep(1000);
                }
                
                
            }
        } catch (InterruptedException e) {

            e.printStackTrace();

        }
    }
    
    public static void main(String[] args) {
        SingleDemo  singleDemo = new SingleDemo();
        Thread thread = new Thread(singleDemo);
        thread.start();

    }

}

效果图:

原文地址:https://www.cnblogs.com/wangmingxia/p/5584216.html