在计时器后刷新jTextField

时间:2014-09-06 12:57:53

标签: java swing jtextfield javax.swing.timer

我正在为Java主题做作业。我使用的是NetBean IDE。我的任务要求我做一个文字游戏。我设计的游戏涉及延迟为1000毫秒的计时器。计时器将变量从30递减到0.计时器本身正在工作。它被放置在GUI类的主要功能中。我面临的问题是,我不知道每次变量递减时我应该如何更新jTextfield。

public static void main(String args[]) {

    Time counter=new Time();
ActionListener actListner = new ActionListener() {
            public void actionPerformed(ActionEvent event) {

            counter.decTime();
             jTime.setText("Time left: " + counter.getTime());


}



 };
          Timer timer = new Timer(1000, actListner);
            timer.start();
    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new StartGUI().setVisible(true);


        }
    });
}

我不确定如何正确实施

jTime.setText("Time left: " + counter.getTime());

1 个答案:

答案 0 :(得分:1)

不确定你做错了什么(这就是你应该总是提供short example that we can copy-paste-compile-run that demonstrates the problem的原因。当我使代码运行时,它运行正常。这就是为什么我们需要能够运行你的< / em>代码,看看你哪里出错了。

这是可运行的版本:

import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.Timer;

public class StartGUI extends JFrame {

    static JTextField jTime = new JTextField(10);

    public StartGUI() {
        jTime.setEditable(false);
        add(jTime);
        setLayout(new GridBagLayout());
        setSize(200, 200);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    static class Time {
        int time = 1000;
        void decTime() {
            time--;
        }
        int getTime() {
            return time;
        }
    }

    public static void main(String args[]) {
        Time counter = new Time();
        ActionListener actListner = new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                counter.decTime();
                jTime.setText("Time left: " + counter.getTime());
            }
        };
        Timer timer = new Timer(1000, actListner);
        timer.start();
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new StartGUI().setVisible(true);
            }
        });
    }
}

这里的代码重构了一些更好的做法

import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.Timer;

public class StartGUI extends JFrame {

    private JTextField jTime = new JTextField(10);
    private Timer timer = createTimer(1000);

    public StartGUI() {
        jTime.setEditable(false);

        add(jTime);
        setLayout(new GridBagLayout());
        pack();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    private Timer createTimer(int delay) {
        Timer timer = new Timer(delay, new ActionListener(){
            Time counter = new Time(30);
            public void actionPerformed(ActionEvent e) {
                if (counter.getTime() == 0) {
                    ((Timer)e.getSource()).stop();
                    jTime.setText("Times up!");
                } else {
                    jTime.setText("Time left: " + counter.getTime());
                    counter.decTime();
                }
            }
        });
        timer.setInitialDelay(0);
        return timer;
    }

    private Timer getTimer() {
        return timer;
    }

    static class Time {
        int time = 1000;
        public Time(int time) {
            this.time = time;
        }
        void decTime() {
            time--;
        }
        int getTime() {
            return time;
        }
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                StartGUI start = new StartGUI();
                start.setVisible(true);
                start.getTimer().start();
            }
        });
    }
}