滚动 java TextArea 的问题
所有,
我在使用 JTextArea 和 JScrollPane 时遇到问题。由于某种原因,滚动窗格似乎无法识别文档中的最后一行,并且只会向下滚动到其前面的行。滚动条甚至不会更改为我可以滑动它的状态,直到文档中的行数比 textArea 显示的行数大两行(一旦大一,就会发生这种情况)。
以前有人遇到过这个吗?什么是一个好的解决方案(我想避免在文档末尾添加额外的“空白”行,每次添加新行时都必须将其删除)?
以下是我实例化 TextArea 和 ScrollPane 的方法:
JFrame frame = new JFrame("Java Chat Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = frame.getContentPane();
if (!(pane.getLayout() instanceof BorderLayout)) {
System.err.println("Error: UI Container does not implement BorderLayout.");
System.exit(-1);
}
textArea = new JTextArea();
textArea.setPreferredSize(new Dimension(500, 100));
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
JScrollPane scroller = new JScrollPane(textArea);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
pane.add(scroller, BorderLayout.CENTER);
这是我用来向 textArea 添加新行的方法:
public void println(String a)
{
textArea.append(" "+a+"\n");
textArea.setCaretPosition(textArea.getDocument().getLength());
}
感谢您的帮助,
乔纳森
编辑:另外,作为旁注,使用当前代码,我必须手动向下滚动。我假设 println(line) 方法中的 setCaretPosition(doc.getLength()) 会在输入一行后自动将页面设置到底部...应该是这种情况,还是我需要做一些不同的事情?
All,
I am running into an issue using JTextArea and JScrollPane. For some reason the scroll pane appears to not recognize the last line in the document, and will only scroll down to the line before it. The scroll bar does not even change to a state where I can slide it until the lines in the document are two greater than the number of lines the textArea shows (it should happen as soon as it is one greater).
Has anyone run into this before? What would be a good solution (I want to avoid having to add an extra 'blank' line to the end of the document, which I would have to remove every time I add a new line)?
Here is how I instantiate the TextArea and ScrollPane:
JFrame frame = new JFrame("Java Chat Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = frame.getContentPane();
if (!(pane.getLayout() instanceof BorderLayout)) {
System.err.println("Error: UI Container does not implement BorderLayout.");
System.exit(-1);
}
textArea = new JTextArea();
textArea.setPreferredSize(new Dimension(500, 100));
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
JScrollPane scroller = new JScrollPane(textArea);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
pane.add(scroller, BorderLayout.CENTER);
Here is the method I use to add a new line to textArea:
public void println(String a)
{
textArea.append(" "+a+"\n");
textArea.setCaretPosition(textArea.getDocument().getLength());
}
Thanks for your help,
Jonathan
EDIT: Also, as a side note, with the current code I have to manually scroll down. I assumed that setCaretPosition(doc.getLength()) in the println(line) method would automatically set the page to the bottom after a line is entered... Should that be the case, or do I need to do something differently?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是
textArea.setPreferredSize(new Dimension(500, 100));
删除它。设置滚动窗格的 perferredSize 或框架的大小
The problem is
textArea.setPreferredSize(new Dimension(500, 100));
Remove that. Set the perferredSize of the scrollpane or the size of your frame instead
这是一个解决方案吗?我没有测试过,所以请不要杀我。
Is this a solution? I didn't test it, so please don't kill me.