简体   繁体   English

JTextPane明文

[英]JTextPane clear text

I'm trying to remove all text from a JTextPane. 我正在尝试从JTextPane中删除所有文本。 I thought you could simply use: 我以为你可以简单地使用:

textPane.setText(""); 

This DOES work, but for some reason, there always is an empty line after calling that method. 这可以工作,但由于某种原因,在调用该方法后总是有一个空行。 Why is that and how do I prevent that? 为什么这样,我该如何防止这种情况?

Probably because you are using a KeyListener to listen for the Enter key and then clear the text. 可能是因为您正在使用KeyListener来侦听Enter键,然后清除文本。 Well JTextPane has an Action which adds a newline character when the Enter key is pressed and the is happening after you clear the text pane. JTextPane有一个Action,当按下Enter键时会添加换行符,清除文本窗格后会发生这种情况。

The proper solution is to use Key Bindings and replace the default Action with an action that clears the text pane. 正确的解决方案是使用键绑定并将默认操作替换为清除文本窗格的操作。

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

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