Java Swing 计时器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1006611/
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
Java Swing Timer
提问by Hamza Yerlikaya
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//...Perform a task...
logger.finest("Reading SMTP Info.");
}
};
Timer timer = new Timer(100 ,taskPerformer);
timer.setRepeats(false);
timer.start();
According to the documentation this timer should fire once but it never fires. I need it to fire once.
根据文档,这个计时器应该触发一次,但它永远不会触发。我需要它开火一次。
回答by kgiannakakis
This simple program works for me:
这个简单的程序对我有用:
import java.awt.event.*;
import javax.swing.*;
public class Test {
public static void main(String [] args) throws Exception{
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//...Perform a task...
System.out.println("Reading SMTP Info.");
}
};
Timer timer = new Timer(100 ,taskPerformer);
timer.setRepeats(false);
timer.start();
Thread.sleep(5000);
}
}
回答by basszero
Your task likely only needs to reportresults on the event thread (EDT) but do the actual work in a background thread at some periodic rate.
您的任务可能只需要在事件线程 (EDT) 上报告结果,但在后台线程中以某种周期性速率执行实际工作。
ScheduledExecutorServiceis EXACTLYwhat you want. Just remember to update the state of your UI on the EDT via SwingUtility.invokeLater(...)
ScheduledExecutorService的是EXACTLY你想要什么。请记住通过 SwingUtility.invokeLater(...) 在 EDT 上更新 UI 的状态
回答by Nick Holt
I'm guessing from the log statement that you're doing some sort of SMTP operation. I think I'm right in saying the java.swing.Timer
is intended for UI related timed operations, hence why it needs and EDT running. For more general operations you should use java.util.Timer
.
我从日志语句中猜测您正在执行某种 SMTP 操作。我认为我说的java.swing.Timer
是正确的,它旨在用于与 UI 相关的定时操作,因此为什么它需要运行 EDT。对于更一般的操作,您应该使用java.util.Timer
.
This article is linked from the JavaDocs - http://java.sun.com/products/jfc/tsc/articles/timer/
本文链接自 JavaDocs - http://java.sun.com/products/jfc/tsc/articles/timer/
回答by nandhakumar.c
This Program will work fine...
该程序将正常工作...
setRepeats(boolean flag)
function used to set call the function(actionPerformed)
repeatedly or only one time if
setRepeats(boolean flag)
用于设置调用function(actionPerformed)
重复或仅一次的函数,如果
timer.setRepeats(false) == timer
calls the actionperformed method for only one timetimer.setRepeats(true) == timer
calls the actionPerformed method repeatedly based on specified time
timer.setRepeats(false) == timer
只调用一次 actionperformed 方法timer.setRepeats(true) == timer
根据指定时间重复调用 actionPerformed 方法
Swing Timer Work
摆动定时器工作
- do the task one time
- do the task repeated time
- 做一次任务
- 做任务重复时间
steps to create swing timer:
创建摇摆计时器的步骤:
- create the actionlistener
- create the timer constructor then pass time and actionlistener in that
- implement the
actionPerformed()
function in which do your task - use
timer.start()
for start the task between the time specified in timer constructor, usetimer.stop()
for stop the task
- 创建动作监听器
- 创建计时器构造函数,然后在其中传递时间和动作侦听器
- 实现执行
actionPerformed()
任务的功能 - 使用
timer.start()
用于启动定时器的构造,使用指定的时间之间的任务timer.stop()
为停止任务
Example:
例子:
ActionListener al=new ActionListener(
public void actionPerformed(ActionEvent ae)
{
//do your task
if(work done)
timer.stop();//stop the task after do the work
}
);
Timer timer=new Timer(1000,al);//create the timer which calls the actionperformed method for every 1000 millisecond(1 second=1000 millisecond)
timer.start();//start the task