简体   繁体   English

将JTextArea强制到JScrollPane的底部

[英]Force JTextArea to the bottom of JScrollPane

I've got the following code: 我有以下代码:

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");
}
}

The code results in the following JScrollPane: 该代码将导致以下JScrollPane:

JScrollPane的

How do I force the JTextArea to the bottom of the JScrollPane so it looks like this (NB I use MigLayout): 我如何将JTextArea强制到JScrollPane的底部,使其看起来像这样(注意,我使用MigLayout):

JScrollPane2

一种方法是根据JTextArea的行数在开头添加空白行,并使用setCaretPosition()方法将插入号位置设置为TextArea的末尾。

To force JTextArea at the end of JScrollPane , you can make use of Layouts. 要在JScrollPane的末尾强制JTextArea ,可以使用Layouts。 BorderLayout will do the job for you. BorderLayout将为您完成这项工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM