猿问

如何在Java GUI中设置JTextArea的自动滚动?

我已经在JScrollPane上嵌入了JTextArea,并且正在使用该JTextArea进行输出。


我希望每当输出超出JTextArea的大小时,JTextArea都会自动滚动,以便用户不必手动向下滚动即可查看最新输出。


我怎样才能做到这一点?


我已经将JTextArea和JScrollPane的autoscroll属性设置为true。


交互式爱情
浏览 916回答 3
3回答

犯罪嫌疑人X

&nbsp;JScrollBar vbar = scrollPane.getVerticalScrollBar();&nbsp; &nbsp; for (int i = 0; i < 20; i++) {&nbsp; &nbsp; &nbsp; &nbsp; myJTxt.append("This is text " + i + "\n");&nbsp; &nbsp; &nbsp; &nbsp; vbar.setValue(vbar.getMaximum());&nbsp; &nbsp; &nbsp; &nbsp; vbar.paint(vbar.getGraphics());&nbsp; &nbsp; &nbsp; &nbsp; myJTxt.scrollRectToVisible(myJTxt.getVisibleRect());&nbsp; &nbsp; &nbsp; &nbsp; myJTxt.paint(myJTxt.getGraphics());&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.sleep(250);&nbsp; &nbsp; &nbsp; &nbsp; } catch (InterruptedException ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Logger.getLogger(ScrollTextView.class.getName()).log(Level.SEVERE, null, ex);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答