0

我想在中追加html内容,但是当我以这种方式追加时,它会在现有文本的末尾自动JEditorPane插入换行符,如何避免这种情况。

JEditorPane pn = new JEditorPane();
pn.setContentType("text/html");
pn.setText("This is line 1");
...
//after some time
HTMLDocument doc = (HTMLDocument) pn.getDocument();
HTMLEditorKit kit = (HTMLEditorKit) pn.getEditorKit();
kit.insertHTML(doc, doc.getLength(), "<b>Hello</b>", 0, 0, null);
kit.insertHTML(doc, doc.getLength(), "World", 0, 0, null);

insertHTML()每次调用它都会在现有文本的末尾放置一个换行符。这是默认行为吗?如果是这样,我该如何处理?

4

2 回答 2

2

HTMLDocument 有方法

public void insertAfterStart(Element elem, String htmlText)
public void insertBeforeEnd(Element elem, String htmlText)
public void insertBeforeStart(Element elem, String htmlText)
public void insertAfterEnd(Element elem, String htmlText)

您可以在其中传递要插入的段落或字符元素(叶子)和 html

于 2014-04-07T13:54:07.013 回答
-1

可能不是最好的方法,但你可以试试这个:

pn.setText(pn.getText + "your text to add here");
于 2014-04-07T12:31:49.970 回答