简体   繁体   English

关于事件调度线程

[英]About event dispatch thread

I'm reading some articles about Event Dispatch Thread in java. 我正在阅读有关Java中的事件调度线程的文章。 As it implies the creation of GUI and all the events of GUI components should performed within this thread . 因为它暗示了GUI的创建以及GUI组件的所有事件都应在此线程内执行。 if take a look at some code which generate on net beans IDE when create a JFrame I can agree with a part of this comment. 如果看一下在创建JFrame时在net bean IDE上生成的一些代码,我可以同意此注释的一部分。 Because it includes some code that direct the GUI creation to the EDT like java.awt.EventQueue.invokeLater(new Runnable().. ... 因为它包含一些将GUI创建定向到EDT的代码,例如java.awt.EventQueue.invokeLater(new Runnable().. ...

Ex:
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Temp().setVisible(true);
        }
    });
}

But expressions that we write on events handling methods does not have that kind of directing statements. 但是,我们在事件处理方法上编写的表达式没有这种定向语句。

Ex
private void btnTodayActionPerformed(java.awt.event.ActionEvent evt) {                                         
    xxxxxxxxxxxxxxxxx ;
    xxxxxxxxxxxxxxxxxxxxxxxxxxx;
} 

Where is that? 哪里是? Or is it done by the compiler?? 还是由编译器完成? Please be kind to help me .. this will be some silly question… 请帮助我..这将是一个愚蠢的问题…

As the name implies, all UI events, including click events, are raised on the event dispatch thread. 顾名思义,所有UI事件(包括单击事件)都在事件分发线程上引发。

What the event dispatch thread actually does is run an OS-level message loop to be notified of UI events, then dispatch those events to the appropriate Swing components. 事件分派线程实际执行的操作是运行OS级别的消息循环,以通知UI事件,然后将这些事件分派给适当的Swing组件。

事件总是在EDT上引发,因此您为回调编写的任何代码也都在EDT上,仅当您已经在使用其他线程时才需要invokeLater(或类似代码)(例如,不应在线程上执行长任务) EDT,因为它们冻结了整个接口,相反,它们应该在自己的线程上执行,并定期使用invokeLater更新接口)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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