为什么java swing定时器自动停止?

为什么java swing定时器自动停止?,java,timer,Java,Timer,我正在制作一个简单的应用程序,可以每隔一段时间拍摄屏幕截图。所以我使用了swing定时器。但是当我将时间间隔设置为100毫秒时,我遇到了一个问题,它工作得非常好 Timer t=new Timer(100, new ActionListener()//this is working 但当我将间隔设置为1000时,它不起作用 Timer t=new Timer(1000, new ActionListener()//this is not working 没有错误,但程序自动终止。计时器不启动

我正在制作一个简单的应用程序,可以每隔一段时间拍摄屏幕截图。所以我使用了swing定时器。但是当我将时间间隔设置为100毫秒时,我遇到了一个问题,它工作得非常好

Timer t=new Timer(100, new ActionListener()//this is working
但当我将间隔设置为1000时,它不起作用

Timer t=new Timer(1000, new ActionListener()//this is not working
没有错误,但程序自动终止。计时器不启动。我使用netbeans。我可以看到程序已停止。这是我在控制台中看到的

我不知道我做错了什么。或者我错过了什么

这是我的密码

public class Screenshot {
    Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    private final String s;
    int i=0;
    public Screenshot() {
        SimpleDateFormat sdf=new SimpleDateFormat("M,dd,hh,mm,ss");
        s = sdf.format(new Date());
        System.out.println(s);
        shoot();
        System.out.println("CALLED");
    }

    public final void shoot() {

        Timer t=new Timer(1000, new ActionListener() {//not working with 1000 but work with 100

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    i++;
                    System.out.println(i);
                    BufferedImage capture = new Robot().createScreenCapture(screenRect);
                    ImageIO.write(capture, "jpg", new File("C:\\Users\\Madhawa.se\\Desktop\\screenshot\\"+s+"sh"+i+".jpg"));
                } catch (Exception ex) {
                   ex.printStackTrace();
                }
            }
        });
        t.start();        
    }
    public static void main(String[] args) {
        Screenshot shot=new Screenshot();       
    }    
}

这是因为主线程在计时器线程启动之前就已经退出了。创建Screenshot类的实例后,尝试向main()方法添加wait()

这将阻止main()方法退出。在实际的应用程序中,您可能需要一些允许用户退出程序的逻辑,但这将解决您眼前的问题

这里有一个例子来说明我的意思:

import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Test {

    public Test() {
        start();
    }

    public final void start() {

        Timer t=new Timer(1000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Firing!");
            }
        });
        t.start();        
    }
    public static void main(String... args) {
        Test shot=new Test();   

        synchronized(shot){
            try {
                //this causes the Main thread to block, keeping the program running
                shot.wait();
            } 
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }    
}

这是因为主线程在计时器线程启动之前就已经退出了。创建Screenshot类的实例后,尝试向main()方法添加wait()

这将阻止main()方法退出。在实际的应用程序中,您可能需要一些允许用户退出程序的逻辑,但这将解决您眼前的问题

这里有一个例子来说明我的意思:

import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Test {

    public Test() {
        start();
    }

    public final void start() {

        Timer t=new Timer(1000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Firing!");
            }
        });
        t.start();        
    }
    public static void main(String... args) {
        Test shot=new Test();   

        synchronized(shot){
            try {
                //this causes the Main thread to block, keeping the program running
                shot.wait();
            } 
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }    
}

我要做的第一件事是尝试重新编写代码,以便考虑Swing线程


编辑:实际上,在没有Swing GUI的情况下,甚至不应该使用Swing计时器,因为没有GUI,任何东西都无法保持Swing线程运行。我将使用java.util.Timer来代替它。

我要做的第一件事是尝试重新编写代码,以便考虑Swing线程


编辑:实际上,在没有Swing GUI的情况下,甚至不应该使用Swing计时器,因为没有GUI,任何东西都无法保持Swing线程运行。我会使用java.util.Timer来代替。

我想我不需要swing Timer。它可能是一个线程。但我想知道为什么会这样exit@getlost:显示JOptionPane以启动Swing线程,你会看到代码是有效的。我想我不需要swing定时器。可能是一个线程。但我想知道为什么exit@getlost:显示JOptionPane以启动Swing线程,您将看到代码正常工作。@getlost您确实应该阅读关于线程和Swing并发性的教程。当您编写一个小示例程序来测试它时发生了什么?@getlost您真的应该阅读关于线程和Swing并发性的教程。当您编写一个小示例程序来测试它时发生了什么?