Java Swing - 如何向下滚动JTextArea? [英] Java Swing - how to scroll down a JTextArea?

查看:160
本文介绍了Java Swing - 如何向下滚动JTextArea?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本聊天应用程序。我使用 JTextArea 作为缓冲区。添加消息后,我想滚动到底部。怎么能实现这一目标?

I have an application with basic chat. I use JTextArea for the buffer. After adding a message I want to scroll to the bottom. How can this be achieved?

我发现没有允许我这样做的成员函数。

I found no member function that would allow me to do this.

推荐答案

您可以通过将插入符号位置设置为文本区域的末尾来执行此操作,即

You can do this by setting the caret position to the end of the text area, i.e.,

myTextArea.setCaretPosition(myTextArea.getDocument().getLength());

编辑:你可以通过寻找更多关于这个问题的信息在本页右下方列出的相关问题中。特别是,请查看camickr的答案并链接到这个主题:如何在Java GUI中设置JTextArea的AUTO-SCROLLING?。这是一个比我给出的更好,更完整的答案(我现在刚刚为此投票)。

you can find out a lot more on this question by looking at the related questions listed on the lower right of this page. In particular, please check out camickr's answer and link in this thread: How to set AUTO-SCROLLING of JTextArea in Java GUI?. It's a far better and more complete answer than the one I've given (and I've just now up-voted it for this).

这篇关于Java Swing - 如何向下滚动JTextArea?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆