0

我想我已经在这个程序中得到了 jframe,但是为什么我运行它时什么都没有出现?我有两个不同的课程,这是我的第一个。忽略最后一种方法,我将在其中绘制一个带有圆圈的矩形作为红绿灯。

这是我的代码。

package trafficlight;

import java.awt.Color;
import java.awt.Graphics;

public class TrafficLight {

private int goDuration;
private int stopDuration;
private int warnDuration;

public enum State {STOP, GO, WARN};
public Color GO_COLOR = Color.green;
public Color STOP_COLOR = Color.red;
public Color OFF_COLOR = Color.darkGray;
public Color WARNING_COLOR = Color.yellow;
private State currentState;


public TrafficLight() {
goDuration =  2;
stopDuration = 2;
warnDuration =1;
currentState =  State.GO;
}

public void changeLight(){
if(currentState  == State.GO){
        currentState = State.WARN;
}
if(currentState == State.WARN){
        currentState = State.STOP;
}
if(currentState == State.STOP){
        currentState = State.GO;
}
}


public int getGoDuration() {
    return goDuration;
}

public void setGoDuration(int goDuration) {
    this.goDuration = goDuration;
}

public int getStopDuration() {
    return stopDuration;
}

public void setStopDuration(int stopDuration) {
    this.stopDuration = stopDuration;
}

public int getWarnDuration() {
    return warnDuration;
}

public void setWarnDuration(int warnDuration) {
    this.warnDuration = warnDuration;
}

public State getCurrentState() {
    return currentState;
}

public void setCurrentState(State currentState) {
    this.currentState = currentState;
}
public int getCurrentDuration(){
    int duration = 0;
    if (currentState == State.STOP){
        duration = stopDuration;
    }
    if (currentState == State.GO){
        duration = goDuration;
    }
    if (currentState == State.WARN){
        duration = warnDuration;
    }
    return duration;
}
public void draw(Graphics canvas) {
    canvas.drawRect(125,185,100,250);
    canvas.drawOval(145,200,60,60);
    canvas.drawOval(145,280,60,60);
    canvas.drawOval(145,360,60,60);

    if (currentState == State.STOP){

    }


}

}

这是我的第二节课。

package trafficlight;
import java.awt.*;
import javax.swing.*;

public class TrafficLightDriver extends JFrame {

private static TrafficLight light;

public void message() {
}

public static void main(String[] args) {
    TrafficLightDriver myFrame = new TrafficLightDriver();
    int delay, answer;
    String valueString;

    do {
        valueString = JOptionPane.showInputDialog("What is the green light delay? (1.. 10)");
        light.setGoDuration(Integer.parseInt(valueString));
        valueString = JOptionPane.showInputDialog("What is the yellow light delay? (1.. 10)");
        light.setWarnDuration(Integer.parseInt(valueString));
        valueString = JOptionPane.showInputDialog("What is the red light delay? (1.. 10)");
        light.setStopDuration(Integer.parseInt(valueString));
        for (int i = 1; i <= 10; i++) {
            delay = light.getCurrentDuration();
            Wait.manySec(delay);
            light.changeLight();
            myFrame.repaint();
        }
        answer = JOptionPane.showConfirmDialog(null, "Would you like to run the light again?",
                null, JOptionPane.YES_NO_OPTION);
    } while (answer == JOptionPane.YES_OPTION);
    System.exit(0);
}

@Override
public void paint(Graphics canvas) {
    light.draw(canvas);
}

public TrafficLightDriver() {  //constructor
    setSize(350, 600);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    light = new TrafficLight();
    setVisible(true);
}
}

这是我的等候班

package trafficlight;

public class Wait {

public static void oneSec() {
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        System.err.println(e);
    }
}

public static void manySec(long s) {
    try {
        Thread.sleep(s * 1000);
    } catch (InterruptedException e) {
        System.err.println(e);
    }
}

public static void tenthOfSec(long s) {
    try {
        Thread.sleep(s * 100);
    } catch (InterruptedException e) {
        System.err.println(e);
    }
}
}
4

2 回答 2

1
  1. 您应该避免覆盖paint顶级容器,相反,您应该使用类似的东西JPanel并覆盖它的paintComponent方法。
  2. 绘制过程是一系列链接的方法,所有这些方法都建立在彼此之上以生成输出,一旦您中断此更改,您就会允许引入伪影和出现不规则性。确保您通过始终调用来尊重油漆链super.paintXxx
  3. Swing 是一个单线程框架。也就是说,有一个线程负责处理系统内的所有事件,包括重绘请求。这意味着,如果您出于任何原因阻塞该线程,您将阻止处理任何使您的程序看起来像是挂起的新事件。您还需要确保对 UI 的任何更新都是在此线程的上下文中进行的。

从通读开始

现在,我不知道你Wait的课是如何工作的,所以我不能评论那部分,但你TrafficLight并没有更新自己以反映它的当前状态......

更新...

您还有两种main非常令人困惑的方法。应用程序逻辑似乎在 中TrafficLightDriver,您应该确保在执行程序时正在运行此类。

changeLight您的方法存在逻辑问题

public void changeLight(){
    if(currentState  == State.GO){
        currentState = State.WARN;
    }
    if(currentState == State.WARN){
        currentState = State.STOP;
    }
    if(currentState == State.STOP){
        currentState = State.GO;
    }
}

基本上,这就是说...

如果 currentState 为 GO,则将 currentState 设置为 WARN...
如果 currentState 为 WARN,则将 currentState 设置为 STOP...
如果 currentState 为 STOP,则将 currentState 设置为 GO...

鉴于默认状态是GO,当您调用此方法时,状态将永远不会更改为GO. 相反,您应该使用一个if-else语句

public void changeLight() {
    if (currentState == State.GO) {
        currentState = State.WARN;
    } else if (currentState == State.WARN) {
        currentState = State.STOP;
    } else if (currentState == State.STOP) {
        currentState = State.GO;
    }
}

更新

自己渲染灯光很大程度上取决于个人喜好,例如,我可能想做类似......

switch (getCurrentState()) {
    case GO:
        canvas.setColor(GO_COLOR);
        canvas.drawOval(145,360,60,60);
        break;
    case WARN:
        canvas.setColor(WARNING_COLOR);
        canvas.drawOval(145,280,60,60);
        break;
    case STOP:
        canvas.setColor(STOP_COLOR);
        canvas.drawOval(145,200,60,60);
        break;
}
canvas.setColor(OFF_COLOR);
canvas.drawRect(125,185,100,250);
canvas.drawOval(145,200,60,60);
canvas.drawOval(145,280,60,60);
canvas.drawOval(145,360,60,60);

这将填充活动的灯光,然后将其他所有内容渲染到顶部,因此灯光始终被勾勒出来

于 2013-11-13T22:44:18.243 回答
0

你调用 System.exit()。这会杀死 java VM 并结束程序。删除该行。

如果您想在 JFrame 关闭时控制程序的行为,请使用 frame.setDefaultCloseOperation()。

于 2013-11-13T22:33:08.053 回答