2010-10-01 84 views

回答

18
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的

+0

伟大的答案谢谢! – 2013-12-27 17:18:23

6

放置的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); 

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

+0

几乎没有投票赞成,因为使用GridBagLayout来处理这样一个简单的需求。 – 2010-10-02 01:27:58

+3

千万不要使用GridBagLayout!决不! – 2010-10-02 04:46:03

+2

通过与Swing中的每个其他布局管理器的长时间完全的烦恼,我现在几乎全部使用GridBagLayout。这是我个人历史的一件神器,而不是一个推荐。请确保使用符合您需求的布局管理器。在这种情况下,我认为权重的确切规格明确显示滚动窗格吸收所有大小变化。但是,它实际上并不值得使用复杂的布局管理器来进行简单的布局。 – Zoe 2010-10-03 16:30:28