Java - 没有 GUI 的倒数计时器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12556190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - Countdown timer without GUI
提问by Kyle93
Basically I am making a text based "game" (Not so much a game, more of a way to improve basic java skills and logic). However, as part of it I wish to have a timer. It would count down on the time I wish from the variable to 0. Now, I have seen a few ways to do this with a gui, however, is there a way to do this without a gui/jframe etc.
基本上我正在制作一个基于文本的“游戏”(与其说是游戏,不如说是一种提高基本 Java 技能和逻辑的方法)。但是,作为其中的一部分,我希望有一个计时器。它会倒计时我希望从变量到 0 的时间。现在,我已经看到了一些用 gui 来做到这一点的方法,但是,有没有办法在没有 gui/jframe 等的情况下做到这一点。
So, what I am wondering is. Can you make a count down from x to 0 without using a gui/jframe. If so, how would you go about this?
所以,我想知道的是。你能在不使用 gui/jframe 的情况下从 x 倒数到 0 吗?如果是这样,你会怎么做?
Thanks, once I have some ideas will edit with progress.
谢谢,一旦我有了一些想法,就会随着进度进行编辑。
Edit
编辑
// Start timer
Runnable r = new TimerEg(gameLength);
new Thread(r).start();
Above is how I am calling the thread/timer
以上是我如何调用线程/计时器
public static void main(int count) {
If I then have this in the TimerEg class, the timer complies. However, when compiling the main in the other thread I get.
如果我在 TimerEg 类中有这个,计时器就会遵守。但是,在另一个线程中编译 main 时,我得到了。
Now, am I completely miss-understanding threads and how this would work? Or is there something I am missing?
现在,我是否完全误解了线程以及它是如何工作的?或者有什么我想念的吗?
Error:
错误:
constructor TimerEg in class TimerEg cannot be applied to given types;
required: no arguments; found int; reason: actual and formal arguments differ in length
Found on line Runnable r = new TimerEg(gameLength);
网上找的 Runnable r = new TimerEg(gameLength);
回答by Hovercraft Full Of Eels
Same as with a GUI, you'd use a Timer, but here instead of using a Swing Timer, you'd use a java.util.Timer. Have a look at the Timer APIfor the details. Also have a look at the TimerTask APIsince you would use this in conjunction with your Timer.
与 GUI 一样,您将使用 Timer,但此处不使用 Swing Timer,而是使用 java.util.Timer。有关详细信息,请查看Timer API。还可以查看TimerTask API,因为您会将它与 Timer 结合使用。
For example:
例如:
import java.util.Timer;
import java.util.TimerTask;
public class TimerEg {
private static TimerTask myTask = null;
public static void main(String[] args) {
Timer timer = new Timer("My Timer", false);
int count = 10;
myTask = new MyTimerTask(count, new Runnable() {
public void run() {
System.exit(0);
}
});
long delay = 1000L;
timer.scheduleAtFixedRate(myTask, delay, delay);
}
}
class MyTimerTask extends TimerTask {
private int count;
private Runnable doWhenDone;
public MyTimerTask(int count, Runnable doWhenDone) {
this.count = count;
this.doWhenDone = doWhenDone;
}
@Override
public void run() {
count--;
System.out.println("Count is: " + count);
if (count == 0) {
cancel();
doWhenDone.run();
}
}
}
回答by Curious
You could write your own countdown timer, as simply as:
您可以编写自己的倒数计时器,如下所示:
public class CountDown {
//Counts down from x to 0 in approximately
//(little more than) s * x seconds.
static void countDown(int x, int s) {
while (x > 0 ) {
System.out.println("x = " + x);
try {
Thread.sleep(s*1000);
} catch (Exception e) {}
x--;
}
}
public static void main(String[] args) {
countDown(5, 1);
}
}
Or you could use Java Timer API
或者你可以使用Java Timer API
回答by fthopkins
It is simple to countdown with java..
用java倒计时很简单..
int minute=10,second=60; // 10 min countdown
int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
second--;
// do something with second and minute. put them where you want.
if (second==0) {
second=59;
minute--;
if (minute<0) {
minute=9;
}
}
}
};
new Timer(delay, taskPerformer).start();