0
我下面的代碼顯示使用Java Swing的窗口。問題是當我在eclipse中運行代碼時窗口不顯示。當我將該文件作爲可執行JAR文件導出並運行時,它可以正常工作。 eclipse中是否存在一個可阻止它從那裏運行的bug?
如果有人能告訴我,我錯過了什麼,我將不勝感激。
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);
}
}
適合我。確保日食知道它應該運行這個類,而不是一個'main'方法的其他類 – MadProgrammer 2014-09-02 03:25:34
是的,我在eclipse中運行配置指向正確的程序和類。它只是開始並說它正在運行,但沒有窗口出現。 – 2014-09-02 03:27:59
添加一些工作'System.out.println'語句來跟蹤進度,看它是否無法達到給定的點。嘗試添加一個斷點並對其進行調試 – MadProgrammer 2014-09-02 03:29:15