如果我们不生成窗口,为什么 Timer 不起作用? [英] Why Timer does not work if we do not generate a window?

查看:41
本文介绍了如果我们不生成窗口,为什么 Timer 不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码如下:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.Timer;

public class TimerSample {
  public static void main(String args[]) {
    new JFrame().setVisible(true);
    ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        System.out.println("Hello World Timer");
      }
    };
    Timer timer = new Timer(500, actionListener);
    timer.start();
  }
}

它会生成一个窗口,然后定期在终端(命令提示符)中打印Hello World Timer".如果我评论这一行 new JFrame().setVisible(true); 应用程序不会向命令行打印任何内容.为什么?

It generates a window and then periodically prints "Hello World Timer" in the terminal (Command Prompt). If I comment this line new JFrame().setVisible(true); the application does not print anything to the command line. Why?

添加:

我不确定我是否正确理解了答案.据我了解,计时器启动了一个新线程.这个新线程与主"线程同时存在.当主"线程完成时(当一切都完成并且没有什么可做的时候)整个应用程序被终止(与计时器创建的新"线程一起).对吗?

I am not sure that I understand the answers correctly. As far as I understood, the timer starts a new thread. And this new thread exists simultaneously with the "main" thread. When the "main" thread is finished (when everything is done and there is nothing to do anymore) the whole application is terminated (together with the "new" thread created by the timer). Is right?

添加 2:

上述解释仍然不能解释一切.例如,如果我注释 new JFrame().setVisible(true); 并放入 try {Thread.sleep(20000);} catch(InterruptedException e) {};timer.start() 之后.所以,我有点理解.通过睡眠,我们使主"线程保持忙碌,因此计时器创建的线程可以存在.但是 new JFrame().setVisible(true); 不占据main".据我了解,它创建了自己的线程(如 Timer).那么,为什么JFrame的线程可以在没有主线程的情况下存在,而定时器的线程却不能呢?

The above described explanation still does not explain everything. For example, the program works if I comment the new JFrame().setVisible(true); and put try {Thread.sleep(20000);} catch(InterruptedException e) {}; after the the timer.start(). So, I kind of understand that. With the sleep we keep the "main" thread busy so the thread created by the timer can exist. But new JFrame().setVisible(true); do not occupy the "main". As far as I understand it creates its own thread (like Timer). So, why thread of the JFrame can exist without the main thread and thread of timer cannot?

推荐答案

你没有抓住重点.Timer 独立于您创建的窗口,并且在您注释掉该窗口创建行时它也可以工作.

You're missing the point. The Timer is independent from the window you created and it works also when you comment out that window creation line.

但是,您没有看到的是:您的主程序在 timer.start() 之后退出,因此,您的程序执行终止,并且计时器随之结束.

However, what you failed to see is: your main program exits after timer.start(), hence, your program execution is terminated and along with it goes the timer.

如果您在末尾添加 Thread.sleep(20000);(包括所需的异常处理)或任何其他需要一些时间的代码,您可以验证这一点.然后,即使没有创建任何窗口,您的计时器也能正常工作.

You can verify this if you add Thread.sleep(20000); at the end (including the required exception handling), or any other code that takes some time. Then your timer works just fine even without any window being created.

关键是 JFrame,即使没有显示任何内容,也会保持活动状态,从而使您的计时器保持活动状态.

The point is that the JFrame, even when nothing is displayed, remains active, which in turn keeps your timer alive.

这篇关于如果我们不生成窗口,为什么 Timer 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆