JTextPane 追加新字符串
每篇文章都会回答“如何将字符串附加到 JEditorPane?”的问题。就像
jep.setText(jep.getText + "new string");
我尝试过这样:
jep.setText("<b>Termination time : </b>" +
CriterionFunction.estimateIndividual_top(individual) + " </br>");
jep.setText(jep.getText() + "Processes' distribution: </br>");
结果我得到“终止时间:1000”而没有“进程分布:”
为什么会发生这种情况???
In an every article the answer to a question "How to append a string to a JEditorPane?" is something like
jep.setText(jep.getText + "new string");
I have tried this:
jep.setText("<b>Termination time : </b>" +
CriterionFunction.estimateIndividual_top(individual) + " </br>");
jep.setText(jep.getText() + "Processes' distribution: </br>");
And as a result I got "Termination time : 1000" without "Processes' distribution:"
Why did this happen???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我怀疑这是附加文本的推荐方法。这意味着每次更改某些文本时,您都需要重新解析整个文档。人们这样做的原因可能是因为他们不了解如何使用 JEditorPane。这其中也包括我。
我更喜欢使用 JTextPane,然后使用属性。一个简单的例子可能是这样的:
I doubt that is the recommended approach for appending text. This means every time you change some text you need to reparse the entire document. The reason people may do this is because the don't understand how to use a JEditorPane. That includes me.
I much prefer using a JTextPane and then using attributes. A simple example might be something like:
JEditorPane
就像JTextPane
一样,有一个可用于插入字符串的Document
。将文本附加到 JEditorPane 中所需执行的操作是以下代码片段:
我对此进行了测试,它对我来说效果很好。
doc.getLength()
是您要插入字符串的位置,显然,使用此行您会将其添加到文本的末尾。A
JEditorPane
, just a like aJTextPane
has aDocument
that you can use for inserting strings.What you'll want to do to append text into a JEditorPane is this snippet:
I tested this and it worked fine for me. The
doc.getLength()
is where you want to insert the string, obviously with this line you would be adding it to the end of the text.setText 是设置文本窗格中的所有文本。使用 StyledDocument 接口进行追加,删除等文本。
setText is to set all text in a textpane. Use the StyledDocument interface to append, remove, ans so on text.