问题描述
Baiscilly,我正在为一个项目制作这个游戏,我无法弄清楚如何让计时器工作,这是我的代码尝试.
Baiscilly, I'm making this game for a project, and I can't fiure out how to get timers to work whatsoever, here is my code attempting.
import java.awt.event.*;
import java.util.Timer;
public class Timers {
private int timeLeft = 60;
private void timer() {
while(timeLeft > 0){
int delay = 1000;
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
timeLeft--;
}{
new Timer(delay, taskPerformer).start();
};
};
}
}
}
我不知道自己做错了什么,也不知道接下来要做什么.除此之外,我需要制作一个 Actionlistener
看看用户的答案是否与预设的答案相同.
I don't know what I'm doing wrong or what to do next. As well as this i need to make an Actionlistener
see if the users answer is the same as the preset answer.
import javax.swing.*;
import java.awt.event.*;
...
JTextField Janswer = new JTextField();
Janswer.setBounds(110, 70, 150, 25);
newFrame.add(Janswer);
if Janswer = equations.getanswer(){
score++;
我想如果我付出这么多,我也可以告诉你从哪里得到答案
And I guess if i'm giving this much I may as well give you where it is getting the answers from
public class Equations {
private static String equation;
private int answer;
Problems problem = new Problems();
public Equations() {
equation = problem.getFirstNumber() + " " + problem.getSign() + " " + problem.getSecondNumber();
String sign = problem.getSign();
if (sign.equals("+"))
answer = problem.getFirstNumber() + problem.getSecondNumber();
else if (sign.equals("-"))
answer = problem.getFirstNumber() - problem.getSecondNumber();
else if (sign.equals("*"))
answer = problem.getFirstNumber() * problem.getSecondNumber();
}
public static String getEquations() {
return equation;
}
public int getAnswer() {
return answer;
}
}
感谢大家为我提供的任何帮助,如果我需要以任何方式更改帖子,请告诉我,我是新手!
Thank you all for any help you're able to give me, and just let me know if I need to change my post in any way, I'm new to this!
推荐答案
主要问题是你使用了一个 while-loop
,它正在创建一堆新的 Timer
> s 在每次迭代中都在减少 timeLeft
值.你只需要一个 Timer
.
The main problem is you are using a while-loop
, which is creating a bunch of new Timer
s on each iteration, which are all decrementing the timeLeft
value. You only need a single Timer
.
根据你想要做什么,你可以做类似......
Depending on what you want to do, you could do something like...
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.out.println("Time's up!");
}
};
new Timer(60 * 1000, taskPerformer).start();
这将在 1 分钟内建立一个回调,这可以让您定义超时...
This will establish a call back in 1 minutes time, which can allow you to define a timeout...
知道,如果你想要一个倒数计时器,你可以这样做......
Know, if you want a count down timer, you could do something like...
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
count++;
if (count >= 59) {
((Timer)evt.getSource()).stop();
System.out.println("Time's up!");
}
}
};
new Timer(1000, taskPerformer).start();
基本上计数到 60(从 0)并且每秒更新一次(您必须使用 60 - count
来获取倒计时值)...
Which basically counts to 60 (from 0) and is updated every second (you'd have to use 60 - count
to get the count down value)...
有关详细信息,请参阅如何使用 Swing 计时器
这篇关于Java 计时器和动作监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!