phi*_*294 2 html java formatting append jtextpane
我可以在没有任何 HTML 问题的情况下解析 JTextPane 的内容:
textPane = new JTextPane();
textPane.setContentType("text/html");
textPane.setText(<b>Hello!</b>);
// ...
setVisible(true);
Run Code Online (Sandbox Code Playgroud)
这导致
你好!
但是每当我尝试将字符串附加到 textPane 时,使用
styledDoc = (StyledDocument) textPane.getStyledDocument();
styledDoc.insertString(styledDoc .getLength(), <b>Goodbye!</b>, null );
Run Code Online (Sandbox Code Playgroud)
(如在这个问题中所见),我的输出是
你好!
<b>Goodbye!</b>
(没有空格) - 所以 html 格式被跳过。
如何将字符串附加到我的 JTextPane 对象并保留添加部分的 HTML 格式?
使用例如
HTMLDocument doc=(HTMLDocument) textPane.getStyledDocument();
doc.insertAfterEnd(doc.getCharacterElement(doc.getLength()),"<b>Goodbye!</b>");
Run Code Online (Sandbox Code Playgroud)
或者
HTMLEditorKit kit=(HTMLEditorKit )textPane.getEditorKit();
Run Code Online (Sandbox Code Playgroud)
如果您想插入段落/表格或其他分支元素,请使用该方法
public void insertHTML(HTMLDocument doc, int offset, String html,
int popDepth, int pushDepth,
HTML.Tag insertTag)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4013 次 |
最近记录: |