在这里作为答案给出的许多Swing片段中,主要方法是调用
SwingUtilities#invokeLater
:
public class MyOneClassUiApp { private constructUi() { // Some Ui related Code } public static void main(String[] args) { SwingUtilities.invokelater(new Runnable() { public void run() { new MyOneClassUiApp().constructUi(); } }); } }
但是根据Threads and Swing article,从主线程构建UI是安全的:
A few methods are thread-safe: In the Swing API documentation,
thread-safe methods are marked with this text:This method is thread safe,although most Swing methods are not.
An application’s GUI can often be constructed and shown in the main
thread: The following typical code is safe,as long as no components
(Swing or otherwise) have been realized:
public class MyApplication { public static void main(String[] args) { JFrame f = new JFrame("Labels"); // Add components to // the frame here... f.pack(); f.show(); // Don't do any more GUI work here... } }
那么,是否有一个真正的(线程安全)的原因,通过SwingUtilities#invokeLater
主要构建UI,或者这只是一个习惯,记得在其他情况下做吗?
解决方法
原文地址:https://www.jb51.cc/java/126478.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。