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

有帮助吗?

解决方案

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.html 或者 http://download.oracle.com/javase/tutorial/uiswing/components/scrollpane.html 有关 JScrollPane 的更多详细信息

其他提示

放置一个JScrollPane的JTextArea中的内部,和地点到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);

这仅仅是一个草图,但应说明如何做到这一点。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top