我有带有 html 内容的 JTextPane,我添加了带有我的 CSS 规则的 set StyleSheet。我想在我的 html 元素中插入 JComponent,以便在其上执行 CSS 规则。html会是这样的:
<body>
<div id = 'content'>
<p class = 't'>t</p>
<p class = 'k'>k</p>
<div class = 'hascomp'>
<span class = 'desc'>description</span>
<br/>
// here I want to insert my java component
</div>
</div>
</body>
以下是我用 hascomp 类创建元素的 java 代码:
try {
doc.insertBeforeEnd(content, "<div class = 'hascomp'><span class = 'desc'>"+description+"</span><br/>");
} catch (BadLocationException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int offset = Pane.getDocument().getLength();
Pane.setCaretPosition(offset);
Pane.insertComponent(jcomponent);
try {
doc.insertBeforeEnd(content, "</div>");
} catch (BadLocationException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
hascomp 类的 CSS:
.hascomp {font : 10px monaco; background-color:#E15AE3;margin : 2px;padding : 1px 10px;border-radius: 4px;border-width: 1px;border-style: solid;border-color: #D5D3CD;text-align: left;clear: both;float: left; }
但它不能正常工作。
那么,如何将 JComponent 嵌入到 JTextPane 的 html 元素中?!