简体   繁体   English

如何使JTextArea在Java中可滚动?

[英]How to make a JTextArea scrollable in java?

Hi I have made a JTextArea but it is not scrolling, can someone please tell me why? 嗨,我做了一个JTextArea,但是它没有滚动,有人可以告诉我为什么吗?

JTextArea textArea = new JTextArea(2, 0);
textArea.setText("sdsdsd \n dfdfdf \n dsdsdsdsd \n dsdsdsd \n sdsdsdsd");
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
panel.add(textArea);

Also, I would like it to auto scroll down, when new content gets added to, show it only shows the last 2 lines automatically, if possible. 另外,我希望它能够自动向下滚动,当添加新内容时,显示它仅在可能的情况下自动显示最后两行。

Thanks. 谢谢。

Use, 采用,

panel.add(scrollPane);

not

panel.add(textArea);

Add the JScrollPane to the JPanel, not the JTextArea. 将JScrollPane添加到JPanel,而不是JTextArea。

To Scroll to the bottom, see this answer . 要滚动到底部,请参阅此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM