Java Swing变量未递增 [英] Java Swing variable not incrementing

查看:67
本文介绍了Java Swing变量未递增的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我使用了一个字符串数组来固定电台.然后,我尝试使用JTextField显示火车当前所在的车站.每当火车离开车站时,我都希望增加一个旅程"计数器,以便显示下一个车站.

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.

但是,它不起作用.我假设问题不在于变量不递增,而在于JTextField不更新,而是不管问题是什么,

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,

我不知道该如何解决.

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,不会.

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.

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

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

这篇关于Java Swing变量未递增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆