我正在使用 Java (21) Swing JEditorPane 或 JTextPane 来显示 HTML 字符串。这可以工作,但会显示 HTML 注释。
<!-- Hidden Text -->
。
JEditorPane htmlpane = new JEditorPane();
htmlpane.setContentType("text/html");
htmlpane.setText("<html><table><tr><td>Cell 1</td>"
+ "<td>Cell 2</td></tr><tr><td>Cell 3</td>"
+ "<td>Cell 2 A<!-- Hidden Text --> Cell 2 B</td>"
+ "</tr></table></html>");
panel.add(htmlpane);
(JTextPane 的结果相同)
我想显示 HTML 并隐藏注释。 (一般来说:在 Swing GUI 中显示尽可能接近正确的 HTML。)
您遇到的行为似乎是故意的,因为 JEditorPane 是可编辑的。
请参阅以下有关此主题的 jdk bug 条目:https://bugs.openjdk.org/browse/JDK-6422138
评论中指出:
JEditorPane 应该在可编辑时显示所有内容,因此它显示注释这一事实并不是一个错误。
还有:
关闭可编辑属性以使 HTML 页面正常呈现(就像在浏览器中一样)。
您现在的选择是: