gpt4 book ai didi

Java Swing 变量不递增

转载 作者:行者123 更新时间:2023-12-01 13:06:21 25 4
gpt4 key购买 nike

我正在做的作业要求我构建一个控制和跟踪火车的界面。

我使用了一系列字符串来保存电台。然后我尝试使用 JTextField 显示火车当前所在的车站。每当火车离开车站时,我都希望计数器“旅程”增加,以便显示下一站。

但是,它不起作用。我假设问题不是与变量不递增有关,而是与 JTextField 不更新有关,但无论问题是什么,

我不知道如何解决这个问题。

代码:

import java.awt.*; 
import javax.swing.*;
import java.awt.event.*;

public class Train extends JFrame {

JPanel panel = new JPanel();

boolean poweredOn = false;
boolean doorsOpen = false;
boolean trainMotion = false;
boolean trainReady = false;
int journey = 0;
String stations[] = {"Darlington","North Road","Heighington","Shildon","Newton Aycliffe","Bishop Auckland"};
String output = "This station is " + stations[journey];

JButton start = new JButton("Start Train");
JButton close = new JButton("Close Doors");
JButton open = new JButton("Open Doors");
JButton stop = new JButton("Stop");
JButton go = new JButton("Go");
JButton end = new JButton("Shutdown Train");

JTextArea station = new JTextArea(output);

public static void main(String[] args) {
new Train();
}

void initialState() {
close.setEnabled(false);
open.setEnabled(false);
stop.setEnabled(false);
go.setEnabled(false);
end.setEnabled(false);
}

void startTrain() {
poweredOn = true;
start.setEnabled(false);
open.setEnabled(true);
}

void openDoors() {
if (journey == 5) {
poweredOn = false;
start.setEnabled(true);
journey = 0;
initialState();
}

else {
doorsOpen = true;
open.setEnabled(false);
close.setEnabled(true);
}
}

void closeDoors() {
doorsOpen = false;
close.setEnabled(false);
go.setEnabled(true);
end.setEnabled(true);
trainReady = true;

if (journey == 5) {
open.setEnabled(true);
go.setEnabled(false);
trainReady = false;
}
}

void goTrain() {
journey = journey + 1;
trainMotion = true;
trainReady = false;
stop.setEnabled(true);
end.setEnabled(false);
go.setEnabled(false);
}

void stopTrain() {
trainMotion = false;
open.setEnabled(true);
stop.setEnabled(false);
}

void shutdownTrain() {
trainReady = false;
start.setEnabled(true);
go.setEnabled(false);
end.setEnabled(false);
}

public Train() {

super("Train Control System");
setSize(700,500);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);

start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
startTrain();
}});

open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openDoors();
}});

close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
closeDoors();
}});

go.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
goTrain();
}});

stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
stopTrain();
}});

end.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
shutdownTrain();
}});

panel.add(start);
panel.add(close);
panel.add(open);
panel.add(stop);
panel.add(go);
panel.add(end);
panel.add(station);
add(panel);

initialState();
setVisible(true);

}
}

最佳答案

您的旅程增量很好,问题是您似乎假设更改旅程将如何改变车站<中输出的文本 JTextArea,但它不会。

您需要告诉 station 当您更新 journey 变量时您希望显示什么内容,例如...

journey = journey + 1;
station.setText("This station is " + stations[journey]);

关于Java Swing 变量不递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23233473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com