我想用(Java + Eclipse + Swing)創建我的第一個GUI應用程序。這是我的代碼:爲什麼我的基於Swing的GUI應用程序沒有響應?
import java.awt.*;
import javax.swing.*;
public class HelloWorldSwing extends JFrame {
JTextArea m_resultArea = new JTextArea(6, 30);
//====================================================== constructor
public HelloWorldSwing() {
//... Set initial text, scrolling, and border.
m_resultArea.setText("Enter more text to see scrollbars");
JScrollPane scrollingArea = new JScrollPane(m_resultArea);
scrollingArea.setBorder(BorderFactory.createEmptyBorder(10,5,10,5));
// Get the content pane, set layout, add to center
Container content = this.getContentPane();
content.setLayout(new BorderLayout());
content.add(scrollingArea, BorderLayout.CENTER);
this.pack();
}
//============================================================= main
public static void main(String[] args) {
JFrame win = new HelloWorldSwing();
win.setTitle("TextAreaDemo");
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setVisible(true);
}
}
該代碼取自here。當我從Eclipse運行應用程序時,出現期望的窗口(所以,這很好,我看到了我想看到的東西)。但是,當我嘗試關閉窗口或嘗試在文本區域中寫入內容時,程序會凍結。操作系統寫道,程序沒有響應(我在Ubuntu上試用)。
任何人都可以幫助我找到問題的原因嗎?
非常感謝您的幫助。
我試過提供的代碼,它工作正常...你還有什麼其他的嗎?當你註釋掉構造函數的內容以獲得一個空的'JFrame'並嘗試關閉它時,它是否工作? – 2010-02-17 15:12:13
這不是代碼。它在我的Windows機器上正常工作。按預期關閉並寫入。你不是在用beta jvm嗎? – Oso 2010-02-17 15:33:49
我試着評論這段代碼:content.add(scrollingArea,BorderLayout.CENTER);我仍然有同樣的問題。出現一個空的窗口,它也沒有響應。 – Roman 2010-02-17 15:35:26