了java.awt.EventQueue.invokeLater解释 [英] java.awt.EventQueue.invokeLater explained

查看:727
本文介绍了了java.awt.EventQueue.invokeLater解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇,为什么我们要使用了java.awt.EventQueue.invokeLater 来控制挥杆的组件。

I am very curious why do we have to use java.awt.EventQueue.invokeLater to control swing components.

为什么我们不能这样做,在正常线程?究竟是怎么回事幕后?从我已经注意到,如果我有一个的JFrame 我可以设置能见度从主线程true或false没有得到任何错误,它似乎工作。那么究竟是什么我用了java.awt.EventQueue.invokeLater 实现?我也深知,我可以使用 SwingUtilities.invokeLater 但作为<一href=\"https://stackoverflow.com/questions/8847083/swingutilities-invokelater-vs-eventqueue-invokelater\">explained这里,它们似乎是同一个东西。

Why can't we do that in normal thread? What exactly is going on behind the scenes? From what I have noticed if I have a JFrame I can set visibility to true or false from the main thread without getting any errors, and it does seem to work. So what exactly do I achieve by using java.awt.EventQueue.invokeLater? I am also fully aware that I can use SwingUtilities.invokeLater but as explained here, they seem to be one and the same thing.

由于任何人对他们的解释。希望这是一个有效的问题。

Thanks to anyone for their explanation. Hopefully this is a valid question.

编辑:回答问题wumpz
我们可以创建一个JFrame

to answer wumpz question We can create a jframe

JFrame frame = new JFrame("Hello world");
frame.setSize(new Dimension(300, 300));
frame.setPreferredSize(new Dimension(300, 300));
frame.setMaximumSize(new Dimension(300, 300));
frame.setMinimumSize(new Dimension(300, 300));
frame.setVisible(true);
frame.pack();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

和它创建的同一个线程执行以下操作:

And on the same thread it was created do the following.

for (int i = 0; i < 34; i++)
{
    System.out.println("Main thread setting to "+(!frame.isVisible()));
    frame.setVisible(!frame.isVisible());
}

和没有怨言。

推荐答案

完整的摇摆处理是在一个名为线程的 EDT(事件指派线程)完成。因此,你会遮挡GUI如果你计算这个线程内的一些长期持久的计算。

The complete Swing processing is done in a thread called EDT (Event Dispatching Thread). Therefore you would block the GUI if you would compute some long lasting calculations within this thread.

去这里的路是处理不同的线程中的计算,让你的GUI保持敏感。在最后你想更新你的图形用户界面,其中 有无以是美国东部时间内完成。现在 EventQueue.invokeLater 用武之地。它在秋千事件列表末尾张贴一个事件(你的的Runnable )和所有其他GUI事件被处理后才被处理。

The way to go here is to process your calculation within a different thread, so your GUI stays responsive. At the end you want to update your GUI, which have to be done within the EDT. Now EventQueue.invokeLater comes into play. It posts an event (your Runnable) at the end of Swings event list and is processed after all other GUI events are processed.

还有 EventQueue.invokeAndWait 的使用是可能在这里。所不同的是,你的计算线程阻塞,直到你的GUI被更新。
这不能从EDT使用。

Also the usage of EventQueue.invokeAndWait is possible here. The difference is, that your calculation thread blocks until your GUI is updated. This must not be used from the EDT.

注意的不可以从不同的线程更新Swing GUI的。在大多数情况下,这会产生一些奇怪的更新/刷新的问题。

Be careful not to update your Swing GUI from a different thread. In most cases this produces some strange updating/refreshing issues.

还有Java的code,在那里,开始一个JFrame从主线程简单。这可能会导致问题,而不是从秋千pvented $ P $。大多数现代的IDE现在建立这样的事情来启动GUI:

Still there is Java code out there that starts a JFrame simple from the main thread. This could cause issues, but is not prevented from Swing. Most modern IDEs now create something like this to start the GUI:

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

这篇关于了java.awt.EventQueue.invokeLater解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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