如何停止java swing计时器

btqmn9zl  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(450)

这个应用程序有两个线程,这里显示的一个线程调用一个方法,该方法每4秒暂停一个自动点击器方法(只是为了方便)来执行一些鼠标移动。我希望它停止计时器时,你点击图形用户界面停止按钮。现在,当您点击停止,然后再次启动,然后它有两个计时器,将执行代码;等等。
行动听众的东西。

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方法。
感谢您抽出时间帮助一位新手:)。

2fjabf4q

2fjabf4q1#

下面是一个如何在线程外声明示例并能够在线程外控制它的快速示例。

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实现)。
在这里,我只是启动线程,然后暂停进程几秒钟并停止计时器。
注意,在这里,线程不做任何其他事情,所以它直接结束。你需要稍微考虑一下,以符合你的需要,但你有一个工作的例子。
编辑:(恶魔,如果你发布你的答案,通知我,我会删除这部分)
使用标志(在类中)

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,则从事件(在源代码中)获取计时器并停止它。

相关问题