只有红灯和绿亮灯,黄灯死活不显示.
import java.awt.*;
import java.awt.event.*;
public class JiaoTong extends Frame {
boolean redStatus=false,greenStatus=false,yellowStatus=false;
public void paint(Graphics g) {
Color c=g.getColor();
if(redStatus==true)
{
g.setColor(Color.RED);
g.fillOval(100,50, 50,50);
g.drawString( "红灯停下来 ", 100, 120);
}
else
{
g.setColor(Color.BLACK);
g.fillOval(100,50, 50,50);
}
if(yellowStatus==true){
g.setColor(Color.YELLOW);
g.fillOval(100, 150, 50, 50);
}
else
{
g.setColor(Color.BLACK);
g.fillOval(100, 150, 50, 50);
}
if(greenStatus==true){
g.setColor(Color.GREEN);
g.fillOval(100, 250, 50, 50);
g.drawString( "绿灯行 ", 100, 320);
}
else
{
g.setColor(Color.BLACK);
g.fillOval(100, 250, 50, 50);
}
g.setColor(c);
}
public void Lauch() {
setTitle( "交通灯指示 ");
setSize(300, 400);
setBackground(Color.BLACK);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
new Thread(new PaintThread()).start();
}
public static void main(String[] args) {
JiaoTong a=new JiaoTong();
a.Lauch();
}
public class PaintThread implements Runnable
{
public void run() {
for(int i=0;i <8;i++){
switch (i) {
case 0:
redStatus=true;
greenStatus=false;
yellowStatus=false;
break;
case 4:
redStatus=false;
greenStatus=false;
yellowStatus=true;
case 5:
redStatus=false;
greenStatus=true;
yellowStatus=false;
default:
break;
}
repaint();
if(i==7)
i=-1;
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
------解决方案--------------------
case 4:
redStatus=false;
greenStatus=false;
yellowStatus=true;
加个break;
==============>
case 4:
redStatus=false;
greenStatus=false;
yellowStatus=true;
break;