Question

The assignment I am working on requires me to build an interface that controls and tracks a train.

I used an array of strings to hold on the stations. I then tried using a JTextField to display the station the train is currently at. Whenever the train leaves station, I wanted a counter 'journey' to increment so that the next station would display.

However, it's not working. I'm assuming that the problem isn't to do with the variable not incrementing but rather the JTextField not updating, but regardless of what the problem is,

I don't know how to fix this.

Code:

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

}
}
Was it helpful?

Solution

Your journey increments fine, what's wrong is you seem to be assuming that changing the journey will some how change the text that is output in the station JTextArea, which it won't.

You will need to tell station what content you would like displayed in when you update the journey variable, for example...

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

OTHER TIPS

When you push the stop button (Stop Train), you need to do:

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

The journey variable is being updated correctly. For me though the layout was a little messed up when the station name was changed, so maybe you should work with the layout a bit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top