13

我在 JPanel 中有一个 JTextArea。当 JPanel 调整大小并在输入过多文本时滚动时,如何让 JTextArea 填充整个 JPanel 并调整大小?

4

2 回答 2

21
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());  //give your JPanel a BorderLayout

JTextArea text = new JTextArea(); 
JScrollPane scroll = new JScrollPane(text); //place the JTextArea in a scroll pane
panel.add(scroll, BorderLayout.CENTER); //add the JScrollPane to the panel
// CENTER will use up all available space

有关更多信息,请参阅http://download.oracle.com/javase/6/docs/api/javax/swing/JScrollPane.htmlhttp://download.oracle.com/javase/tutorial/uiswing/components/scrollpane.html JScrollPane 的详细信息

于 2010-10-01T22:50:45.213 回答
7

将 JTextArea 放在 JScrollPane 内,然后将其放入 JPanel 中,并使用固定大小的布局。例如,带有 GridBagLayout 的示例可能如下所示:

JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());

JScrollPane scrollpane = new JScrollPane();
GridBagConstraints cons = new GridBagContraints();
cons.weightx = 1.0;
cons.weighty = 1.0;
panel.add(scrollPane, cons);

JTextArea textArea = new JTextArea();
scrollPane.add(textArea);

这只是一个粗略的草图,但它应该说明如何做到这一点。

于 2010-10-01T22:50:54.207 回答