我正在复习期末考试,这是一个练习题,要求我让形状可见半秒钟然后看不见半秒钟。我正在考虑使用计时器来继续这种闪烁的行为。
Timer count = new Timer(500, someAction)
但是这种计时器需要一个ActionListener和一个ActionPerformed来启动这个计时器。
无论如何我可以在没有任何动作的情况下启动计时器吗?
答案 0 :(得分:4)
我仍然认为Timer
解决方案是完美的
Timer timer = new Timer( 500, new ActionListener(){
@Override
public void actionPerformed( ActionEvent e ){
//toggle visible flag of the shape
//trigger a repaint
}
} );
timer.setRepeats( true );
timer.start();
请注意,您可以安全地调整形状的可见标记(将在绘画期间使用),因为Timer
将在EDT上调用ActionListener
。 javax.swing.Timer
的主要优点之一,非常适合与Swing组件结合使用: - )