java刷新jTextField,跟随计时器
我正在做一个Java主题的作业。我正在使用NetBeanIDE。我的作业要求我做一个文字游戏。我正在设计的游戏包含一个延迟为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 楼答案
不确定你做错了什么(这就是为什么你应该总是提供一个short example that we can copy-paste-compile-run that demonstrates the problem。当我让代码可以运行时,它工作得很好。这就是为什么我们需要能够运行你的代码,看看你哪里出了错
以下是可运行版本:
下面是经过一些更好实践重构的代码