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

我使用了一个字符串数组来固定工作站。然后,我尝试使用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);

}
}

最佳答案

您的journey可以递增,这是错误的,您似乎在假设更改journey会在某种程度上更改station JTextArea中输出的文本,而不会。

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

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

10-01 00:32