Java JFrame 不显示在 Eclipse 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23695439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JFrame does not show up in Eclipse
提问by July
I tried to execute the following code within Eclipse (OSX):
我尝试在 Eclipse (OSX) 中执行以下代码:
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.setSize(new Dimension(400, 30));
frame.add(new JButton("hello"));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
The frame does not show up but I get the following console messages:
该框架没有显示,但我收到以下控制台消息:
2014-05-16 14:45:35.230 java[8685:903] [Java CocoaComponent compatibility mode]: Enabled
2014-05-16 14:45:35.232 java[8685:903] [Java CocoaComponent compatibility mode]: Setting timeout for SWT to 0.100000
2014-05-16 14:45:35.546 java[8685:903] *** __NSAutoreleaseNoPool(): Object 0x100612800 of class NSConcreteMapTableValueEnumerator autoreleased with no pool in place - just leaking
2014-05-16 14:45:35.547 java[8685:903] *** __NSAutoreleaseNoPool(): Object 0x100613f40 of class __NSCFDate autoreleased with no pool in place - just leaking
2014-05-16 14:45:35.547 java[8685:903] *** __NSAutoreleaseNoPool(): Object 0x100616e60 of class NSCFTimer autoreleased with no pool in place - just leaking
2014-05-16 14:45:35.550 java[8685:903] *** __NSAutoreleaseNoPool(): Object 0x10061d7c0 of class __NSCFDate autoreleased with no pool in place - just leaking
2014-05-16 14:45:35.550 java[8685:903] *** __NSAutoreleaseNoPool(): Object 0x10061e610 of class NSCFTimer autoreleased with no pool in place - just leaking
While, if I put the code into a java file outside of my Eclipse project and compile and run it via command line, everything is fine and the frame shows up. Can anybody help me troubleshooting?
同时,如果我将代码放入 Eclipse 项目之外的 java 文件中并通过命令行编译和运行它,一切都很好,框架会显示出来。有人可以帮我排除故障吗?
UPDATE
更新
the code now looks like this:
代码现在看起来像这样:
import javax.swing.*;
public class TestFrame {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
private void createAndShowGUI() {
JFrame frame = new JFrame("Test");
System.out.println(SwingUtilities.isEventDispatchThread());
frame.getContentPane().add(new JButton("hello"));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
});
}
}
This does not solve the problem (as already mentioned by user DSquare). I figured out that it seems to be a problem with that specific Eclipse project. If I create a new Eclipse project (same Eclipse), the code runs without an error message and the frame shows up. I still don't have a clue which project cnfigurations may cause it. I don't have swt.jar in my classpath (though org.eclipse.swt and org.eclipse.swt.cocoa.macosx.x86_64 in my plugin-dependencies).
这并不能解决问题(正如用户 DSquare 已经提到的)。我发现这似乎是该特定 Eclipse 项目的问题。如果我创建一个新的 Eclipse 项目(同一个 Eclipse),则代码运行时不会出现错误消息,并且会显示框架。我仍然不知道哪些项目配置可能会导致它。我的类路径中没有 swt.jar(尽管我的插件依赖项中有 org.eclipse.swt 和 org.eclipse.swt.cocoa.macosx.x86_64)。
采纳答案by July
I'm not sure whether this is the final solution but I learned a few things and want to share this here:
我不确定这是否是最终解决方案,但我学到了一些东西,并想在这里分享:
- it matters that I tried to run the code within my plugin
- the plugin is written for an eclipse based application
- therefore the plugin contains an SWT-dependency (I realized that it seemes to be a conflict between swt and swing/awt if people had similar problems)
- I found this article https://www.eclipse.org/articles/article.php?file=Article-Swing-SWT-Integration/index.html
- and this example code how to implement swing GUI by using the SWT/AWT Bridge http://www.java2s.com/Tutorial/Java/0280__SWT/EmbededSwingAWTcomponentstoSWT.htm
- the example runs within my plugin without any problems and I will try to adapt my source code to use the SWT/AWT Bridge
- I'll keep on reading about this :-)
- 我试图在我的插件中运行代码很重要
- 该插件是为基于 Eclipse 的应用程序编写的
- 因此该插件包含一个 SWT 依赖项(我意识到如果人们有类似的问题,它似乎是 swt 和 swing/awt 之间的冲突)
- 我发现这篇文章https://www.eclipse.org/articles/article.php?file=Article-Swing-SWT-Integration/index.html
- 以及这个示例代码如何使用 SWT/AWT 桥实现 Swing GUI http://www.java2s.com/Tutorial/Java/0280__SWT/EmbededSwingAWTcomponentstoSWT.htm
- 该示例在我的插件中运行没有任何问题,我将尝试调整我的源代码以使用 SWT/AWT 桥
- 我会继续阅读这个:-)
Any comment/correction is very welcome (as I'm still a newbie to all this...)
非常欢迎任何评论/更正(因为我仍然是这一切的新手......)
回答by SiMuRg
Your code is right and working in my own eclipse.
您的代码是正确的并且在我自己的日食中工作。
May be your eclipse is not working perfectly. in my opinion change your eclipse version and try again.
可能是你的日食工作不完美。在我看来,请更改您的 eclipse 版本并重试。
By the way your make sure to jdk version is same in eclipse and your console.
顺便说一下,您确保 eclipse 和控制台中的 jdk 版本相同。
回答by Eypros
I don't think you can directly add component to the JFrame. You must use the syntax:
我认为您不能直接向 JFrame 添加组件。您必须使用以下语法:
frame.getContentPane().add(new JButton("hello"));
as mentioned by @DSquare.
正如@DSquare 所提到的。
You can add directly to other swing component though but not JFrame.
您可以直接添加到其他摆动组件,但不能直接添加到 JFrame。
I did a little more research and find out that since java 1.5 you can call add()
directly to the JFrame
object and it will implicitly call the correct pane. That include only the addition step. All other calls to content pane should be done explicitly.
我做了更多的研究,发现从 java 1.5 开始,你可以add()
直接调用JFrame
对象,它会隐式调用正确的窗格。这仅包括加法步骤。对内容窗格的所有其他调用都应明确完成。
回答by adriannieto
You need to add frame.pack() before frame.setVisible(true).
您需要在 frame.setVisible(true) 之前添加 frame.pack()。
Also check DSquare comment: http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/start/HelloWorldSwingProject/src/start/HelloWorldSwing.java
还要检查 DSquare 评论:http://docs.oracle.com/javase/tutorial/displayCode.html?code= http://docs.oracle.com/javase/tutorial/uiswing/examples/start/HelloWorldSwingProject/src/start /HelloWorldSwing.java
回答by Kristoff Shijia Yan
I had a similar problem when I was using swing and java 1.8. There is some kind of threading problem as mentioned by @DSquare.
我在使用 Swing 和 java 1.8 时遇到了类似的问题。@DSquare 提到了某种线程问题。
I finally solved the problem by unchecking the "Use the -XstartOnFirstThread argument when launching with SWT" in Run --> Run Configurations --> Arguments
我终于通过在运行 --> 运行配置 --> 参数中取消选中“使用 SWT 启动时使用 -XstartOnFirstThread 参数”解决了这个问题
Hope this helps.
希望这可以帮助。