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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:11:35  来源:igfitidea点击:

Java Swing Timer

javaswingtimer

提问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.Timeris 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)重复或仅一次的函数,如果

  1. timer.setRepeats(false) == timercalls the actionperformed method for only one time
  2. timer.setRepeats(true) == timercalls the actionPerformed method repeatedly based on specified time
  1. timer.setRepeats(false) == timer只调用一次 actionperformed 方法
  2. timer.setRepeats(true) == timer根据指定时间重复调用 actionPerformed 方法

Swing Timer Work

摆动定时器工作

  1. do the task one time
  2. do the task repeated time
  1. 做一次任务
  2. 做任务重复时间

steps to create swing timer:

创建摇摆计时器的步骤:

  1. create the actionlistener
  2. create the timer constructor then pass time and actionlistener in that
  3. implement the actionPerformed()function in which do your task
  4. use timer.start()for start the task between the time specified in timer constructor, use timer.stop()for stop the task
  1. 创建动作监听器
  2. 创建计时器构造函数,然后在其中传递时间和动作侦听器
  3. 实现执行actionPerformed()任务的功能
  4. 使用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