实现代码:
import javax.swing.*;
public class TestSwing extends JFrame {
public TestSwing() {
// 若没有这行代码也没有窗口事件监听,单击关闭按钮窗口将隐藏,不会关闭
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane sp = new JScrollPane();
JTextArea ta = new JTextArea(100, 50);
sp.getViewport().add(ta);
this.getContentPane().add(sp);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Starting TestMyButton...");
TestSwing mainFrame = new TestSwing();
mainFrame.setSize(400, 400);
mainFrame.setTitle("TestButton");
mainFrame.setVisible(true);
}
}
运行结果: