何时使用SwingUtilies.invokeAndWait / invokeLater [英] When to use SwingUtilies.invokeAndWait/invokeLater

查看:447
本文介绍了何时使用SwingUtilies.invokeAndWait / invokeLater的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读的地方,对于任何影响gui的视觉效果的线程,应该在EDT中使用SwingUtilities.invokeAndWait / invokeLater

I read somewhere that for any thread that affects the visuals of the gui it should be ran in the EDT using SwingUtilities.invokeAndWait/invokeLater

对于基本的gui,是否需要在EDT的行中使用invokeAndWait插入新的SwingGUI()。setVisible(true); 只是为了显示吗?

For a basic gui, is it necessary to put something like new SwingGUI().setVisible(true); in the line of the EDT using invokeAndWait? Just to display?

这是否算?

推荐答案

你的问题的简短答案是:是的,即使调用 setVisible 应该在EDT上发生。要确定当前线程是否为EDT,您可以使用 EventQueue#isDispatchThread 方法

The short answer to your question is: yes, even calling setVisible should happen on the EDT. To find out whether the current thread is the EDT, you can use the EventQueue#isDispatchThread method

一些参考链接:

  • Multithreaded Swing Applications
  • Threads and Swing
  • Concurrency in Swing

编辑:
在阅读我提供的链接后,似乎Oracle网站上的一些文章已过时,因为它们仍然是文档,您可以在另一个线程上创建Swing组件。有一个 stackoverflow问题,其中包含一些很好的答案和链接到博客和有关新政策(新的几年前)的文章

after reading the links I provided, it seems some of the articles on the Oracle site are outdated as in they still document you can create Swing components on another thread. There is a stackoverflow question on this which contains some nice answers and links to blogposts and articles about the 'new' policy (new as in a few years old)

这篇关于何时使用SwingUtilies.invokeAndWait / invokeLater的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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