我将使用代码演示我想要实现的目标。
创建计时器参考
Timer timer;
构造和初始化计时器
timer = new Timer(duration, timerAction);
timer.start();
现在timerListener
AbstractAction timerAction = new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
//Perform some action for duration
jlabel.setText("A"); //action_1
//See description under code
jlabel.setText("B"); // action_2
}
};
寻求的方案:
等等。
action_1仅在action_2完成其间隔后才会执行后续间隔。
在示例中描述它:让我们采取这些行动
jlabel.setText("A"); //action_1
jlabel.setText("B"); // action_2
等等。
action_1仅在action_2完成其间隔后才会执行后续间隔。
...
关于如何实现它的任何想法?
答案 0 :(得分:1)
也许是这样的:
public class DoubleActionTimer {
private final Action action1;
private final Action action2;
private final int delay1;
private final int delay2;
private final Timer timer;
private DoubleActionTimer(Action action1, int delay1, Action action2, int delay2) {
this.timer = new Timer(delay1, new ActionSwitcher());
this.action1 = action1;
this.delay1 = delay1;
this.action2 = action2;
this.delay2 = delay2;
this.timer.setRepeats(false);
this.timer.start();
}
public void stop() {
this.timer.stop();
}
private class ActionSwitcher extends AbstractAction {
private boolean flag = false;
/**
* Invoked when an action occurs.
*/
@Override
public void actionPerformed(ActionEvent e) {
final Action action = flag?action2:action1;
final int delay = flag?delay1:delay2;
flag = !flag;
action.actionPerformed(e);
timer.setInitialDelay(delay);
timer.start();
}
}
public static void main(String[] args) throws InterruptedException {
final Action action1 = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Action1"+new Date());
}
};
final Action action2 = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Action2 "+new Date());
}
};
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new DoubleActionTimer(action1, 500, action2, 3000);
}
});
TimeUnit.SECONDS.sleep(60);
}
}