java - 我想我的 jframe 在这个程序中是正确的,但不确定

标签 java swing

我认为这个程序中的 jframe 是正确的,但是为什么当我运行它时什么也没有出现? 我有两个不同的类(class),这是我的第一个。只需忽略最后一个方法,我将在其中绘制一个带有圆圈的矩形作为红绿灯。

这是我的代码。

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);
}
}

这是我的等待类(class)

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);
    }
}
}

最佳答案

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

从通读开始

现在,我不知道您的 Wait 类是如何工作的,所以我无法评论该部分,但是您的 TrafficLight 不会更新自身以反射(reflect)它的情况目前状态...

已更新...

您还有两个非常令人困惑的 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);

这将填充处于 Activity 状态的灯光,但随后将其他所有内容渲染到顶部,因此灯光始终处于轮廓中

关于java - 我想我的 jframe 在这个程序中是正确的,但不确定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19965803/

相关文章:

java - 如何修复不明确的方法引用?

java - 将类(class)保存到 firebase

java - 为什么 ArrayList 的性能明显优于 LinkedList?

java - 创建类似于 native Windows 外观和感觉的 Java GUI

java - JTextPane/JTextField 罕见字符的奇怪行为

java - jTable 单元格中的新行

java.lang.NullPointerException : Attempt to invoke interface method 'OnColorChangeListener.colorChanged(java.lang.String)' on a null object reference 异常

java - 在设置布局之外添加元素(即 JLabels)

java - perl 到 java 程序的持续响应

java - 将 JSON 数组转换为 Java 类对象列表