与 Java Swing 计时器的混淆
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13503788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Confusion with the Java Swing Timer
提问by dreamcrash
I am really confused with the Java swing timer. Does it need an action listener? Could someone please give me an example? Thanks!
我真的对 Java 摆动计时器感到困惑。它需要一个动作监听器吗?有人可以给我一个例子吗?谢谢!
回答by Eng.Fouad
Here is an example of using javax.swing.Timer
:
这是使用的示例javax.swing.Timer
:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class SimpleTimer extends JFrame
{
private JLabel label;
private Timer timer;
private int counter = 10; // the duration
private int delay = 1000; // every 1 second
private static final long serialVersionUID = 1L;
public SimpleTimer()
{
super("Simple Timer");
setDefaultCloseOperation(EXIT_ON_CLOSE);
label = new JLabel("Wait for " + counter + " sec", JLabel.CENTER);
JPanel contentPane = (JPanel) getContentPane();
contentPane.add(label, BorderLayout.CENTER);
contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
pack();
ActionListener action = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{
if(counter == 0)
{
timer.stop();
label.setText("The time is up!");
}
else
{
label.setText("Wait for " + counter + " sec");
counter--;
}
}
};
timer = new Timer(delay, action);
timer.setInitialDelay(0);
timer.start();
setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new SimpleTimer();
}
});
}
}
回答by dreamcrash
Setting up a timer involves creating a Timer object, registering one or more action listenerson it, and starting the timer using the start method. For example, the following code creates and starts a timer that fires an action event once per second (as specified by the first argument to the Timer constructor). The second argument to the Timer constructor specifies a listener to receive the timer's action events. Timers are constructed by specifying both a delay parameter and an ActionListener. (source)
设置计时器包括创建一个Timer 对象,在其上注册一个或多个动作侦听器,并使用 start 方法启动计时器。例如,以下代码创建并启动一个计时器,该计时器每秒触发一次操作事件(由 Timer 构造函数的第一个参数指定)。Timer 构造函数的第二个参数指定一个侦听器来接收计时器的动作事件。定时器是通过指定一个延迟参数和一个 ActionListener 来构造的。(来源)
Futhermore, the timer's timing is done in thread distinct from the event dispatch thread (or EDT) which is the thread that runs the code in the ActionListener. So even if the actionPerformed code is slow, the timer will keep on firing regardless and will queue its actionPerformed code on the event queue which will likely get backed up and the event thread will get clogged and the application will be unresponsive or poorly responsive, unsell you set coalesceto true
(source).
此外,计时器的计时是在与事件调度线程(或 EDT)不同的线程中完成的,事件调度线程是在 ActionListener 中运行代码的线程。因此,即使 actionPerformed 代码很慢,计时器仍会继续触发,并将其 actionPerformed 代码排在事件队列中,该队列可能会被备份,事件线程将被阻塞,应用程序将无响应或响应不佳,未售出您将合并设置为true
(源)。
Here is a good tuturial How to Use Swing Timers
这是一个很好的教程How to Use Swing Timers