我想我已经在这个程序中得到了 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);
}
}
}