Java Swing JEditorPane : manipulating styled documents

标签 java swing jeditorpane

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

我试图在 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/

相关文章:

java - 什么是NullPointerException,我该如何解决?

Java Eclipse Jtable 单元格/列

java - 间接获取命令行参数

java - 如何编辑 JComboBox 并在 JEditorPane 中选择文本

java - JTable、JComboBox - 在第二列中显示 JComboBox 时出现问题

java - 我的 JPanel 类没有在我的 JFrame 上绘制矩形?

java - 调整 JFrame 中 JEditorPane 的大小

java - JEdi​​torPane 不想显示在 html 中完成的按钮(它显示在浏览器中)

java - 使用大量 if 语句缩短 while 循环

java - 在 Play 1.2 中,有没有办法在不编辑模块源代码的情况下保护模块提供的 Controller ?