java - 如何设置 Swing 计时器在启动时执行

标签 java swing timer listeners

我有以下代码来显示 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/

相关文章:

java - 如何识别NAT后面的Server Socket端口

java - jtextarea 数组的声明问题

ios - iOS 12 中通知中心下拉时计时器会中断

swift - 如何检测自上次在 macOS 中输入 NSTextView 后用户不活动?

java - 向 JPanel 添加打印按钮

Java,构造函数以数组作为参数以及来自驱动程序的用户输入

java - 在JAVA中打印多页

java - 调度程序可以挂起一个线程并执行另一个线程/工作吗?

javascript - 停止计时器不工作:

java - : '#i' when parsing c grammar 处的 token 识别错误