我正在尝试使用(Java + Eclipse + 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);
}
}
代码取自这里。
当我从 Eclipse 运行应用程序时,会出现预期的窗口(所以,这很好。我看到了我想看到的)。但是,当我尝试关闭窗口或尝试在文本区域中写入内容时,程序会冻结。操作系统告诉我程序没有响应(我在 Ubuntu 上尝试过)。
谁能帮我找出问题的原因?
预先感谢您的任何帮助。