Ser*_*hiy 12 html java swing document jtextpane
关于我正在制作的一些简单的控制台,我有一个问题.我知道可以setText()使用之前设置的函数将html内容添加到JTextPane setContentType("text/html");.但是对于我的应用程序的需要,我需要直接使用javax.swing.text.Document,我得到了getDocument()函数(例如,删除行并附加新行,是的,它是我正在制作的控制台,我已经在之前的StackOverflow问题中看到了几个例子,但它们都没有满足我的需求).所以,我想要的是将HTML插入到文档中并在我的JTextPane上正确呈现它.问题是当我用insertString()方法(属于文档)添加HTML内容时,JTextPane没有渲染它,在输出中我看到所有的html标记.有没有办法让这个工作正常?
这就是我插入文本的方式:
text_panel = new JTextPane();
text_panel.setContentType("text/html");
//...
Document document = text_panel.getDocument();
document.insertString(document.getLength(), line, null);
text_panel.setCaretPosition(document.getLength());
Run Code Online (Sandbox Code Playgroud)
dog*_*ane 26
您需要使用HTMLEditorKit插入.
JTextPane text_panel = new JTextPane();
HTMLEditorKit kit = new HTMLEditorKit();
HTMLDocument doc = new HTMLDocument();
text_panel.setEditorKit(kit);
text_panel.setDocument(doc);
kit.insertHTML(doc, doc.getLength(), "<b>hello", 0, 0, HTML.Tag.B);
kit.insertHTML(doc, doc.getLength(), "<font color='red'><u>world</u></font>", 0, 0, null);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14243 次 |
| 最近记录: |