简体   繁体   中英

why java swing timer auto stop?

I'm making a simple application to take screenshot in intervals.so i used swing timer for this .but i have a problem when i set interval to 100 millisecond it works perfectly .

Timer t=new Timer(100, new ActionListener()//this is working

but when i set interval to 1000 it's not working.

Timer t=new Timer(1000, new ActionListener()//this is not working

no errors but program automatically terminate.timer not fire.i use netbeans.i can see program has stop .this is what i see in the console.

在此处输入图片说明

i can't figure out what have i done wrong .or have i miss something ??

this is my code

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();       
    }    
}

This is because your main Thread is exiting before your Timer thread even starts. Try adding a wait() to the main() method after you create the instance of the Screenshot class.

This will prevent your main() method from exiting. In a real application you might want some logic that allows the user to exit the program, but this will fix your immediate problem.

Here's an example of what I mean:

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();
            }
        }
    }    
}

First thing I would do would be to try to re-write the code so that it takes Swing threading into consideration.

Edit: And actually a Swing timer should not even be used in a situation without a Swing GUI since nothing will keep the Swing thread going without the GUI. I would use a java.util.Timer instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM