java 显示倒计时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13805569/
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
Display a countdown
提问by user1315906
I need a timer implemented in my application, which will do a countdown from 10 sec - 0 sec.
and, display the countdown in a JLabel
.
我需要在我的应用程序中实现一个计时器,它将从 10 秒到 0 秒进行倒计时。并且,在JLabel
.
Here's my implementation;
这是我的实现;
...
Timer t = new Timer(1000, new List());
t.start();
}
class List implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
int sec = 0;
label.setText(""+sec);
// Do a if- condition check to see if the clock has reached to, and then stop
}
}
I was expecting the JLabel to start counting from 0 - 10 and then stop. But it doesn't. The JLabel set the value 0
and it doesn't get incremented.
我期待 JLabel 从 0 - 10 开始计数,然后停止。但事实并非如此。JLabel 设置了该值0
并且它不会增加。
UPDATE 1
更新 1
t = new Timer(1000, new Listner());
t.start();
}
class Listner implements ActionListener{
private int counter = 0;
@Override
public void actionPerformed(ActionEvent e) {
lable.setText(""+ (counter++));
if (counter == 10)
t.removeActionListener(this);
}
}
回答by Fyre
Well each time the timer is called it declares the int variable sec to 0. Hence the Label doesnt get updated.
那么每次调用计时器时,它都会将 int 变量 sec 声明为 0。因此标签不会更新。
You should declare the sec variable as a global variable and then in the actionPerformed method increment its value each time it is called.
您应该将 sec 变量声明为全局变量,然后在 actionPerformed 方法中每次调用它时都会增加它的值。
public int sec = 0;
class List implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
sec++;
label.setText(""+sec);
// Do a if- condition check to see if the clock has reached to, and then stop
}
}
回答by Hyman
You are not storing nor incrementing secs
anywhere so I don't see how it should get updated, try with
您没有secs
在任何地方存储或递增,所以我不知道它应该如何更新,请尝试
Timer timer;
void start() {
timer = new Timer(1000,new List());
}
class List implements ActionListener {
private counter = 0;
@Override
public void actionPerformed(ActionEvent e) {
label.setText(""+counter++);
if (counter == 10)
timer.removeActionListener(this);
}
}
Mind that you need to store a reference to the timer somewhere to be able to remove the listener from it once countdown finished.
请注意,您需要在某处存储对计时器的引用,以便能够在倒计时完成后从中删除侦听器。
回答by vels4j
A complete example
一个完整的例子
public class ATimerExample {
Timer timer;
int counter = 0;
public ATimerExample() {
final JFrame frame = new JFrame("somethgi");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JLabel label = new JLabel("0");
JPanel panel = new JPanel();
panel.add(label, BorderLayout.SOUTH);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setText(String.valueOf(counter));
counter++;
if (counter == 10) {
//timer.removeActionListener(this);
timer.stop();
}
}
});
timer.start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ATimerExample();
}
});
}
}
回答by Grcy
Since java reads the time in milliseconds, it should be 10000 instead of 1000. try your code and see if that works. I had the same problem when I wanted 30 seconds. And instead of writing Timer t = new Timer(30000, new List()); t.start();
由于 java 以毫秒为单位读取时间,因此它应该是 10000 而不是 1000。试试你的代码,看看它是否有效。当我想要 30 秒时,我遇到了同样的问题。而不是写 Timer t = new Timer(30000, new List()); t.start();
I wrote Timer t = new Timer(3000, new List()); t.start();
我写了 Timer t = new Timer(3000, new List()); t.start();
That made my program to stop every after 3 seconds. I would suggest, you use 10000 instead of 1000.
这使我的程序每 3 秒停止一次。我建议您使用 10000 而不是 1000。
Remember to do: t.stop() in your List class. Thanks
记得在你的 List 类中做: t.stop() 。谢谢