java - 在运行时修改 Swing 计时器的延迟

标签 java swing queue timer delay

我正在开发一个队列模拟,使用 Swing Timer 在一定时间后使对象出队。间隔是通过查看队列中的下一个对象、从中获取一个整数并设置其相应计时器的延迟来确定的。

这是程序中的相关片段(注意:_SECONDS_PER_ITEM 是在其他地方定义为 2000 的常量):

// stop the timer
qTimer[q].stop();

// peek at how many items the customer has, and set the delay.
qTimer[q].setDelay(customerQueue[q].peek().getItems()*_SECONDS_PER_ITEM);

// the next time around, this method will see the flag, and dequeue the customer.
working[q] = true;

// denote that the customer is active on the UI.
lblCustomer[q][0].setBorder(new LineBorder(Color.RED, 2));

// start the timer.
qTimer[q].start();

我遇到的问题是,每个客户,无论他们有多少商品,都会在一秒钟内得到处理。

我应该使用其他方法或技术来设置延迟吗?

最佳答案

看起来,当 stop() 定时器时,用于触发下一个事件的延迟是初始延迟。因此,在上面的示例中使用的正确方法是 setInitialDelay():

{
// stop the timer
qTimer[q].stop();

// peek at how many items the customer has, and set the delay.
qTimer[q].setInitialDelay(customerQueue[q].peek().getItems()*_SECONDS_PER_ITEM);

// the next time around, this method will see the flag, and dequeue the customer.
working[q] = true;

// denote that the customer is active on the UI.
lblCustomer[q][0].setBorder(new LineBorder(Color.RED, 2));

// start the timer.
qTimer[q].start();

}

关于java - 在运行时修改 Swing 计时器的延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/930798/

相关文章:

java - 使用 TLSv1 甚至协议(protocol)配置为 SSL_TLSv2 的应用程序

java - 权衡类的数量与可读性

java - 在 Android 应用程序中使用谷歌翻译

线程 "main"java.lang.NullPointerException 七人骰子游戏中的 JAVA 异常

java - Swing 表格中单元格编辑(单击或双击)的最佳实践

java - 用于响应元素进入的数据结构?

java - Apache Wicket - 自定义 servlet

java - 如何消除java动画闪烁

java - 在 do while 循环中获取要从队列中删除的项目

data-structures - 堆栈、队列和链表