在运行时修改摆动定时器的延迟

时间:2009-05-30 22:41:41

标签: java swing queue timer delay

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

以下是该计划的相关摘要(注意:_SECONDS_PER_ITEM2000其他地方定义的常量):

// 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();

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

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

1 个答案:

答案 0 :(得分:3)

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();

}