Java Swing JEditorPane:操作样式文档 [英] Java Swing JEditorPane: manipulating styled documents
问题描述
我的模型是与枚举类型关联的字符串队列.
I have model that is a queue of Strings associated with enum types.
我正在尝试在 JEditorPane 中显示该模型,队列中的每个元素都作为一个单独的 HTML 段落,该段落具有基于关联枚举类型的属性.
I'm trying to display that model in a JEditorPane, with each element in the queue as a separate HTML paragraph that has attributes based based on the associated enum type.
然而,我的更新方法并没有达到我想要的效果.我尝试将 HTML 字符串直接写入文档(例如,我使用字符串,添加 <p style="color:red">
并附加 </p>
code> 然后将它们插入到文档的末尾),但这给了我输出中的 html 标签(而不是格式) - 这当然与将标签放在我使用构造的字符串上的结果不一致带有 JEditorPane("text/html",String foo) 的文档.我也试过用 AttributeSet 插入,但显然我也做错了.
However, my updating methods are not doing what I want. I tried writing the HTML strings directly to the document (e.g., I take the Strings, prepend <p style="color:red">
and append </p>
and then insert them at the end of the document), but that gives me the html tags in the output (instead of as formatting) - which of course is inconsistent with the result of putting the tags on the string that I use construct the document with JEditorPane("text/html",String foo). I've also tried inserting with an AttributeSet, but apparently I'm doing that wrong as well.
有什么建议吗?
推荐答案
我从来没有在 JEditorPane 中玩过 HTML.我只是在 JTextPane 中使用属性.类似的东西:
I've never had much luck playing with HTML in a JEditorPane. I just use attributes in a JTextPane. Something like:
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:操作样式文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!