我正在尝试使用外部 CSS 样式表来为我在 JEditorPane 中显示的网页设置默认标记属性。我正在尝试使用 Java 的 StyleSheet 类来覆盖在我的网页链接到的外部样式表中定义的特定标记属性。我正在使用以下代码:
editorPane = new JEditorPane();
editorPane.setEditable(false);
editorPane.setContentType("text/html");
try
{
editorPane.setPage(new URL("file:///c:/users/ryan/desktop/htmlviewer.htm"));
}
catch (IOException e)
{
e.printStackTrace();
}
HTMLEditorKit editorKit = new HTMLEditorKit();
editorPane.setEditorKit(editorKit);
HTMLDocument document = (HTMLDocument) editorKit.createDefaultDocument();
StyleSheet styleSheet = document.getStyleSheet();
styleSheet.addRule("p { color:#0000ff; }");
editorKit.setStyleSheet(styleSheet);
add(editorPane);
我的外部 CSS 样式表包含以下行:
p { 颜色:#ff0000; }
当我在 Java 6 中运行它时,p 标签的文本颜色是#0000ff。但是当我在 Java 7 中运行它时,p 标签的文本颜色是#ff0000。
为什么行为发生了变化?有没有办法让我在 Java 中定义的 CSS 属性优先于在我的外部 CSS 样式表中定义的那些?示例代码将不胜感激。