[英]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 . 我正在制作一个简单的应用程序以间隔拍摄屏幕截图。所以我为此使用了摆动定时器。但是当我将间隔设置为100毫秒时,我遇到了问题,它可以完美地工作。
Timer t=new Timer(100, new ActionListener()//this is working
but when i set interval to 1000 it's not working. 但是当我将时间间隔设置为1000时,它不起作用。
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. 没有错误,但程序会自动终止。计时器不会触发。我使用netbeans。我可以看到程序已停止。这就是我在控制台中看到的内容。
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. 这是因为您的主线程甚至在您的Timer线程启动之前就已退出。 Try adding a wait() to the main() method after you create the instance of the Screenshot class.
创建Screenshot类的实例后,尝试将wait()添加到main()方法。
This will prevent your main() method from exiting. 这将防止您的main()方法退出。 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. 我要做的第一件事是尝试重新编写代码,以便将Swing线程考虑在内。
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. 编辑:实际上,没有Swing GUI的情况下甚至不应使用Swing计时器,因为没有GUI的任何东西都不会使Swing线程继续运行。 I would use a java.util.Timer instead.
我会改用java.util.Timer。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.