2

我有一个模型,它是与枚举类型关联的字符串队列。

我试图在 JEditorPane 中显示该模型,队列中的每个元素都作为一个单独的 HTML 段落,该段落具有基于关联枚举类型的属性。

但是,我的更新方法没有做我想要的。我尝试将 HTML 字符串直接写入文档(例如,我获取字符串、前置<p style="color:red">和附加</p>,然后将它们插入到文档的末尾),但这给了我输出中的 html 标签(而不是格式化) -这当然与将标签放在字符串上的结果不一致,我使用 JEditorPane("text/html",String foo) 构造文档。我也尝试过使用 AttributeSet 插入,但显然我也做错了。

有什么建议么?

4

1 回答 1

1

我从来没有在 JEditorPane 中玩过 HTML。我只是在 JTextPane 中使用属性。就像是:

SimpleAttributSet keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.RED);
StyleConstants.setBackground(keyWord, Color.YELLOW);
StyleConstants.setBold(keyWord, true);

try
{
    doc.insertString(doc.getLength(), "\nSome more text", keyWord );
}
catch(Exception e) {}
于 2009-12-05T19:55:28.043 回答