我有以下代码来显示 swing 应用程序中的当前时间。
private void startClock(StateBar stateBar)
{
ActionListener listener = new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
stateBar.setTime(Clock.currentDateStr());
}
};
timer = new Timer(1000,listener);
timer.start();
}
但它似乎不是在计时器启动时执行监听器代码,而是在启动后一秒后执行。 我的意思是,当我用时钟打开面板时,日期显示会延迟 1 秒。是否可以在计时器启动时立即触发执行我的代码?
最佳答案
public void setInitialDelay(int initialDelay)
- Sets the Timer's initial delay, the time in milliseconds to wait after the timer is started before firing the first event. Upon construction, this is set to be the same as the between-event delay, but then its value is independent and remains unaffected by changes to the between-event delay.
使用0
关于java - 如何设置 Swing 计时器在启动时执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24995723/