Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何停止java swing计时器_Java_Multithreading_Swing_Timer_Runnable - Fatal编程技术网

如何停止java swing计时器

如何停止java swing计时器,java,multithreading,swing,timer,runnable,Java,Multithreading,Swing,Timer,Runnable,这个应用程序有两个线程,这里显示的一个线程调用一个方法,该方法每4秒暂停一个自动点击器方法(只是为了方便),以进行一些鼠标移动。我希望它在您单击gui停止按钮时停止计时器。 现在,当你点击停止,然后再次启动,它就会有两个计时器来执行代码;等等 行动听众的东西 class MyButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) {

这个应用程序有两个线程,这里显示的一个线程调用一个方法,该方法每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
它反复调用time重载方法


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

下面是一个快速示例,介绍如何在线程之外声明实例并能够在线程之外控制它

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,如果您发布您的答案,请通知我,我将删除此部分)

使用标志(在类中)


调用
Main.stop()。或者,如果需要停止勾号,您可以使用
布尔值检查每个勾号itself@AxelH我试过我“认为”你的意思,但似乎没有找到正确的方法。如果你不介意的话,举个例子就好了。我可以,但我需要一个例子。你能提供一个只使用线程和计时器的例子吗,没有GUI或者
autoClick
实例吗?我想你想要的是这样的:
如果(!bool){((Timer)evt.getSource()).stop();}
@DevilsHnd,这是我想到的秒级解决方案,你可以根据它发布答案(欢迎您使用我在回答中使用的示例)很抱歉,我有它的内容。但是,如果有变量(bool),我想停止计时器切换为false。我一直在尝试在System.out.println的actionPerformed中放置一个if,以测试bool是否为true,如果不是stop timer。@Hades,好吧,下次精确到这一点,这不在你的问题中。但我已经编辑了我的答案以使用此解决方案。
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;
    }
}