【发布时间】:2014-10-26 05:52:54
【问题描述】:
我在下面有这段代码来显示一个使用 Java Swing 的窗口。问题是当我在 Eclipse 中运行代码时,窗口不显示。当我将文件导出为可执行 JAR 文件并运行它时,它可以工作。 eclipse 是否存在阻止它从那里运行它的错误?
我错过了什么?
package com.gui;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
public class Calculator {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calculator window = new Calculator();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Calculator() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
//frame.setBounds(100, 100, 450, 300);
frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lblNewLabel = new JLabel("Hello World");
frame.getContentPane().add(lblNewLabel, BorderLayout.NORTH);
}
}
【问题讨论】:
-
对我来说很好。确保 Eclipse 知道它应该运行这个类,而不是使用
main方法的其他类 -
是的,我在 Eclipse 中运行配置指向正确的程序和类。它只是启动并说它正在运行,但没有出现任何窗口。
-
添加一些工作
System.out.println语句来跟踪进度,看看它是否未能达到给定点。尝试添加断点并调试它 -
当我将它导出为可运行的 jar 文件然后运行它时它可以工作。知道为什么它在运行时不能在 Eclipse 中正常工作吗?
-
听起来还是 eclipse 运行了错误的类...