如何制作定时器? [英] How to make a Timer?
本文介绍了如何制作定时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想制作等于400 MSc的计时器
然后去打印hi! (例如。)。我知道如何通过 javax.swing.Timer
I want to make a Timer
that waits 400 MSc and then goes and prints "hi !" (e.g.). I know how to do that via javax.swing.Timer
ActionListener action = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println("hi!");
}
};
加:
timer = new Timer(0, action);
timer.setRepeats(false);
timer.setInitialDelay(400);
timer.start();
但据我所知,这绝对不是一种好方法,因为这种定时器
适用于Swing工作。如何以正确的方式做到这一点? (不使用 Thread.sleep()
)
but as I know this definitely is not a good way as this kind of Timer
is for Swing works. how to do that in it's correct way? (without using Thread.sleep()
)
推荐答案
Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("Hi!");
}
}, 400);
这篇关于如何制作定时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文