你如何创建一个立即触发的javax.swing.Timer,然后每隔t毫秒? [英] How do you create a javax.swing.Timer that fires immediately, then every t milliseconds?

查看:120
本文介绍了你如何创建一个立即触发的javax.swing.Timer,然后每隔t毫秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我的代码看起来像这样:

Right now, I have code that looks something like this:

Timer timer = new javax.swing.Timer(5000, myActionEvent);

根据我所看到的(以及 计时器类的Javadocs ) ,计时器将等待5000毫秒(5秒),触发动作事件,等待5000毫秒,再次触发,依此类推。但是,我试图获得的行为是计时器启动,事件被触发,计时器等待5000毫秒,再次触发,然后等待再次触发。

According to what I'm seeing (and the Javadocs for the Timer class), the timer will wait 5000 milliseconds (5 seconds), fire the action event, wait 5000 milliseconds, fire again, and so on. However, the behavior that I'm trying to obtain is that the timer is started, the event is fired, the timer waits 5000 milliseconds, fires again, then waits before firing again.

除非我遗漏了某些东西,否则我没有办法创建一个在开火前不等待的计时器。是否有一种好的,干净的方式来模拟这个?

Unless I missed something, I don't see a way to create a timer that doesn't wait before firing. Is there a good, clean way to emulate this?

推荐答案

您只能在构造函数中指定延迟。您需要更改初始延迟(触发第一个事件之前的时间)。你不能在constuctor中设置,但你可以使用 setInitialDelay 方法的类。

You can only specify the delay in the constructor. You need to change the initial delay (the time before firing the first event). You cannot set in the constuctor, but you can use the setInitialDelay method of the Timer class.

如果你在第一次开火前不需要等待:

If you need no wait before the first firing:

timer.setInitialDelay(0);

这篇关于你如何创建一个立即触发的javax.swing.Timer,然后每隔t毫秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆