【发布时间】:2011-04-20 02:23:42
【问题描述】:
我在 JPanel 中有一个 JTextArea。当 JPanel 调整大小并在输入过多文本时滚动时,如何让 JTextArea 填充整个 JPanel 并调整大小?
【问题讨论】:
标签: java swing jpanel autoresize jtextarea
我在 JPanel 中有一个 JTextArea。当 JPanel 调整大小并在输入过多文本时滚动时,如何让 JTextArea 填充整个 JPanel 并调整大小?
【问题讨论】:
标签: java swing jpanel autoresize jtextarea
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
有关 JScrollPane 的更多详细信息,请参阅 http://download.oracle.com/javase/6/docs/api/javax/swing/JScrollPane.html 或 http://download.oracle.com/javase/tutorial/uiswing/components/scrollpane.html
【讨论】:
将 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);
这只是一个粗略的草图,但它应该说明如何做到这一点。
【讨论】: