【问题标题】:Force JTextArea to the bottom of JScrollPane强制 JTextArea 到 JScrollPane 的底部
【发布时间】:2014-04-02 19:37:02
【问题描述】:

我有以下代码:

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:

如何强制 JTextArea 到 JScrollPane 的底部,使其看起来像这样(注意,我使用 MigLayout):

【问题讨论】:

标签: java swing jscrollpane jtextarea


【解决方案1】:

一种方法是在开头添加空行,具体取决于 JTextArea 的行数,并使用 setCaretPosition() 方法将插入符号位置设置为 TextArea 的末尾。

【讨论】:

    【解决方案2】:

    要在JScrollPane 的末尾强制JTextArea,您可以使用布局。 BorderLayout 将为您完成这项工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-08
      • 1970-01-01
      • 2017-03-05
      • 1970-01-01
      • 1970-01-01
      • 2012-01-18
      • 1970-01-01
      相关资源
      最近更新 更多