2014-03-01 64 views
1

我有下面的代碼:力的JTextArea到JScrollPane的底部

public class LobbyView extends JPanel 
{ 
    this.setLayout(new MigLayout()); 
    private final JTextArea chatWindowMessage = new JTextArea(); 
    private final JScrollPane chatWindow = new JScrollPane(chatWindowMessage); 

LobbyView(Window sentWindow) 
{ 
    chatWindow.setViewportView(chatWindowMessage); 
    chatWindow.setPreferredSize(new Dimension(300, 300)); 
    chatWindow.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 

    chatWindowMessage.setLineWrap(true); 
    chatWindowMessage.append("test" + "\n"); 
    chatWindowMessage.append("test 2" + "\n"); 

    this.add(chatWindow, "pos 180px 75px"); 
    this.add(chatMessage, "pos 180px 380px"); 
} 
} 

的代碼導致以下JScrollPane中:

JScrollPane

如何強制JTextArea中來JScrollPane的底部,所以它看起來是這樣的(注:我使用MigLayout):

JScrollPane2

+0

的borderlayout的文本區域和所有其餘嵌套的borderlayout中心是很酷? –

+0

可能重複[如何更改一個JTextArea文本的位置(http://stackoverflow.com/questions/16800125/how-to-change-the-texts-position-in-a-jtextarea) – Reimeus

回答

0

一種方法是在開始時添加空行,具體取決於JTextArea的行數,並使用setCaretPosition()方法將插入位置設置爲TextArea的末尾。

0

要強制JTextAreaJScrollPane結束後,你可以利用佈局。 BorderLayout會爲你做這項工作。