我有一个模型,它是一个与枚举类型相关联的字符串队列。
我试图在 JEditorPane 中显示该模型,队列中的每个元素都作为一个单独的 HTML 段落,该段落具有基于关联枚举类型的属性。
但是,我的更新方法并没有按照我的意愿进行。我尝试将 HTML 字符串直接写入文档(例如,我使用字符串,在 <p style="color:red">
之前添加并附加 </p>
然后将它们插入到文档的末尾),但这在输出中给了我 html 标签(而不是格式化)——这当然与将标签放在字符串上的结果不一致,我使用 JEditorPane("text/html",String foo) 构造文档。我也试过使用 AttributeSet 插入,但显然我也做错了。
有什么建议吗?
最佳答案
我从来没有在 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) {}
关于Java Swing JEditorPane : manipulating styled documents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1853029/