我试图在 JScrollPane 中显示一个 JTextArea,但是当我运行我的(简化的)程序时,我只是得到一个空框架:
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class ScrollPaneTest extends JFrame {
private Container myCP;
private JTextArea resultsTA;
private JScrollPane scrollPane;
public ScrollPaneTest() {
setSize(500, 500);
setLocation(100, 100);
myCP = this.getContentPane();
myCP.setLayout(null);
resultsTA = new JTextArea("Blah blah");
scrollPane = new JScrollPane(resultsTA,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(200, 100));
scrollPane.setLocation(100, 300);
myCP.add(scrollPane);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args) {
new ScrollPaneTest();
}
}
我使用 null LayoutManager 与我正在教的教科书保持一致。