我有以下代码:
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):
最佳答案
一种方法是在开头添加空行,具体取决于 JTextArea 的行数,并使用 setCaretPosition() 方法将插入符位置设置到 TextArea 的末尾。
关于java - 强制 JTextArea 位于 JScrollPane 的底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22118238/