写入 JTextArea 的方法
目前我有一个方法,它使用字符串缓冲区在运行时写出字符串。然后该方法返回该字符串,然后我将其显示在 JTextArea
中。有没有另一种方法可以做到这一点,以便在该方法运行时将字符串写入文本区域,而不是在完成后写入。谢谢。
Currently I have a method that uses a string buffer to write out a string as it runs. The method then returns this string which I then make appear in a JTextArea
. Is there another way that I can do this so that as the method runs the string gets written to the text area instead of once it has finished. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以向写入字符串缓冲区的方法添加一个 javax.swing.text.Document 参数。然后,当您调用该方法时,传入
JTextArea.getDocument()
的值 — 该对象表示JTextArea
中的文本。然后,每次将文本附加到字符串缓冲区时,也将其附加到文档,如下所示:
请注意,这可能会使您现有的字符串缓冲区变得多余...实际上,您正在使用文档 对象作为字符串缓冲区。另请注意,您可以打赌这种技术将比在显示字符串缓冲区之前将其完全写出慢得多。
You could add a
javax.swing.text.Document
parameter to the method that writes to the string buffer. Then when you call the method, pass in the value ofJTextArea.getDocument()
— this object represents the text in theJTextArea
.Then each time you append text to the string buffer, also append it to the
Document
like this:Note that this might make your existing string buffer redundant... effectively you're using the
Document
object as a string buffer instead. Note also that you can bet this technique will be much, much slower than writing out the string buffer in full before displaying it.