【问题标题】:How to stop a java swing timer如何停止 java swing 计时器
【发布时间】:2017-05-18 08:31:06
【问题描述】:

此应用程序有 2 个线程,此处显示的线程调用一个方法,该方法每 4 秒暂停一次自动点击器方法(只是为了方便)以进行一些鼠标移动。我希望它在您单击 gui 停止按钮时停止计时器。 现在,当您点击停止然后重新启动时,它有两个将执行代码的计时器;等等。

动作监听器的东西。

class MyButtonListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(view.getBtnStart()))
        {
            autoClick.unTerminate();
            bool = true;
            getInfo();
        }
        if (e.getSource().equals(view.getBtnExit()))
        { 
            System.exit(0);
        }
        if (e.getSource().equals(view.getBtnStop()))
        {
            bool = false;
            autoClick.terminate();
        }
    }//end of actionPerformed
}//end of inner class

线程

Thread t2 = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Timer timer = new Timer(4000, new ActionListener() {//301000 5minutes and 1second
                            @Override
                            public void actionPerformed(ActionEvent evt) {
                                autoClick.timeOverload();
                            }                                
                        });
                        //if (!bool){timer.stop();}
                        timer.setRepeats(true); //false only repeates once
                        timer.start();
                    }
                    });//end of t2

它反复调用timeOverload方法。

感谢您抽出宝贵时间帮助新手 :)。

【问题讨论】:

  • 如果你在final 线程之外构建你的计时器以便能够stop 他,这将做到这一点。或者您可以使用boolean 来检查每个刻度是否需要stop 本身
  • @AxelH 我尝试了我“认为”你的意思,但我似乎没有找到正确的方法。如果你不介意举个例子就好了。
  • 我可以,但我需要一个minimal reproducible example。您能否提供一个仅使用线程和计时器、没有 GUI 或 autoClick 实例的示例?
  • 你在寻找什么我认为是这样的:if (!bool) { ((Timer)evt.getSource()).stop(); }
  • @DevilsHnd,这是我想到的秒解决方案,您可以基于此发布答案(欢迎您使用我在答案中使用的示例)

标签: java multithreading swing timer runnable


【解决方案1】:

这里是一个快速示例,说明如何在线程之外声明一个实例并能够在它之外控制它。

public static void main(String[] args){
    final Timer timer = new Timer(500, new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            System.out.println("tick");
        }
    });
    timer.setRepeats(true);

    Thread t = new Thread(new Runnable() {

        public void run() {
            timer.start();
        }
    });
    t.start();

    try {
        Thread.sleep(2600);
    } catch (InterruptedException ex) {
        ex.printStackTrace();
    }

    timer.stop();
}

基本上,您需要将实例final 设置为在匿名类中使用(ActionListener 实现)。

在这里,我只是启动线程,然后暂停进程几秒钟并停止计时器。

请注意,这里的线程不做任何其他事情,所以它直接结束。您需要稍微调整一下以符合您的需求,但您有一个可行的示例。

编辑:(DevilsHnd,如果您发布答案,请通知我,我将删除这部分)

使用标志(在类中)

public class Main {

    public static void main(String[] args){
        new Main();
    }

    boolean running = true;

    public Main(){
        final Timer timer = new Timer(500, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if(!running){
                    ((Timer)e.getSource()).stop();
                } else {
                    System.out.println("tick");
                }
            }
        });
        timer.setRepeats(true);
        timer.start();

        try {
            Thread.sleep(2600);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }

        stop();
    }

    public void stop(){
        running = false;
    }
}

调用Main.stop() 会将标志设置为 false,在执行的每个操作时,检查此标志,如果为 false,则从事件(在源中)获取计时器并停止它。

【讨论】:

  • 很抱歉给您带来了麻烦,但我有。但是假设我想在变量(布尔)切换为 false 时停止计时器。我一直在尝试在 actionPerformed 中的 System.out.println 周围放置一个 if 来测试 bool 是否为真,如果不是,则停止计时器。
  • @Hades,好吧,下次准确,这不是你的问题。但我已编辑我的答案以使用此解决方案。
猜你喜欢
  • 2012-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-23
  • 2018-06-23
  • 2015-04-04
相关资源
最近更新 更多