设置JFrame可见时的JFace错误:Cocoa AWT:在AppKit线程0上运行时不需要

时间:2020-12-27 22:22:06

I'm trying to code my first JFrame for a simple app. The problem is that as soon as I uncomment the setVisible(true); I obtain the following error message:

我正在为一个简单的应用程序编写我的第一个JFrame。问题是,一旦我取消对setVisible(true)的注释;我得到以下错误信息:

Cocoa AWT: Running on AppKit thread 0 when not expected.

Cocoa 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);
    }
}

2 个解决方案

#1


0  

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(...):

您可以使用swinguse来包装代码。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教程中有关并发性的部分。

#2


0  

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. For reference:

我将其归类为Eclipse bug。供参考:

  • 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开普勒服务发行版2 (20140224-0627)
  • I'm on a Mac OS X Yosemite
  • 我在Mac OS X Yosemite上

Please let me know if it helps you or if you have a better solution.

如果对你有帮助,或者你有更好的解决办法,请告诉我。

#1


0  

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(...):

您可以使用swinguse来包装代码。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教程中有关并发性的部分。

#2


0  

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. For reference:

我将其归类为Eclipse bug。供参考:

  • 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开普勒服务发行版2 (20140224-0627)
  • I'm on a Mac OS X Yosemite
  • 我在Mac OS X Yosemite上

Please let me know if it helps you or if you have a better solution.

如果对你有帮助,或者你有更好的解决办法,请告诉我。