简体   繁体   English

将JFrame设置为可见时出现JFace错误:可可AWT:在非预期的情况下在AppKit线程0上运行

[英]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: 以供参考:

  • I'm using the current JDK 1.8.0_31 我正在使用当前的JDK 1.8.0_31
  • I was using Eclipse Kepler Service Release 2 (20140224-0627) 我正在使用Eclipse Kepler Service Release 2(20140224-0627)
  • I'm on a Mac OS X Yosemite 我在Mac OS X优胜美地上

Please let me know if it helps you or if you have a better solution. 请让我知道它是否对您有帮助或您有更好的解决方案。

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

相关问题 Java jframe 在线程中运行代码时冻结 - Java jframe freezing when running code in thread 调用方法以设置可见的JFrame时出错 - Error when calling method to set a JFrame visible 打开 JFrame 时出现 AWT-EventQueue-0 异常 - AWT-EventQueue-0 Exception when openning JFrame 当我从Netbeans运行JFrame程序时,印地语字体是可见的,但运行.jar文件时,则看不见 - Hindi font is visible when I am running JFrame program from Netbeans but not visible when running .jar file 在Eclipse中运行Java应用程序时,为什么JFrame不可见? - Why is my JFrame not visible when running my Java application in eclipse? 运行 JFrame 代码时出现此错误? - When running JFrame Code I get this error? 在bash上运行arduino时(通过Linux的Windows子系统),线程“ AWT-EventQueue-0”中的异常java.awt.HeadlessException - Exception in thread “AWT-EventQueue-0” java.awt.HeadlessException when running arduino on bash (via Windows subsystem of Linux) 在AWT线程中运行代码 - Running code in AWT Thread 制作 JFrame 时抛出 LWJGL java.awt.HeadlessException - LWJGL java.awt.HeadlessException thrown when making a JFrame 单击按钮时创建的项目在 JFrame 中不可见 - items are not visible in JFrame when created on button click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM