[英]JFace error when setting JFrame visible: Cocoa AWT: Running on AppKit thread 0 when not expected
I'm trying to code my first JFrame for a simple app. 我正在尝试为一个简单的应用程序编写我的第一个JFrame。 The problem is that as soon as I uncomment the setVisible(true);
问题是,一旦我取消注释setVisible(true); I obtain the following error message:
我收到以下错误消息:
Cocoa AWT: Running on AppKit thread 0 when not expected. 可可AWT:意外时在AppKit线程0上运行。
Config: Running eclipse on a mac OS 10.10 (Yosemite) and Java is up to date. 配置:在Mac OS 10.10(Yosemite)和Java上运行Eclipse是最新的。
Here is the code: 这是代码:
package gui;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MainFrame extends JFrame {
private JLabel appTitle;
public MainFrame(){
super("Tabum by Team Alpha");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200,500);
setLayout(new BorderLayout());
setVisible(true);
}
}
I've never used your tools, but I guessing that the problem is that all GUI code should execute on the Event Dispatch Thread (EDT) and your code is not doing this. 我从未使用过您的工具,但是我想问题是所有的GUI代码都应该在事件调度线程(EDT)上执行,而您的代码却没有这样做。
You do this by wrapping your code in a SwingUtilities.invokeLater(...): 您可以通过将代码包装在SwingUtilities.invokeLater(...)中来实现此目的:
EventQueue.invokeLater(new Runnable()
{
public void run()
{
// add your code here
}
});
Read the section from the Swing tutorial on Concurrency for more information. 阅读Swing 并发教程中的有关更多信息的部分。
I couldn't get the solution working with Eclipse on my Mac. 我无法在Mac上使用Eclipse解决方案。
However, when I loaded the exact same solution in IntelliJ IDEA, it worked! 但是,当我在IntelliJ IDEA中加载完全相同的解决方案时,它起作用了!
I would classify this as a Eclipse bug. 我将其归类为Eclipse错误。 For reference:
以供参考:
Please let me know if it helps you or if you have a better solution. 请让我知道它是否对您有帮助或您有更好的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.