JTextPane/JEditorPane 显示 HTML <!--comment-->

问题描述 投票:0回答:1

我正在使用 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);

HTML Pane with hidden Text

(JTextPane 的结果相同)

我想显示 HTML 并隐藏注释。 (一般来说:在 Swing GUI 中显示尽可能接近正确的 HTML。)

java html swing jtextpane jeditorpane
1个回答
0
投票

您遇到的行为似乎是故意的,因为 JEditorPane 是可编辑的。

请参阅以下有关此主题的 jdk bug 条目:https://bugs.openjdk.org/browse/JDK-6422138

评论中指出:

JEditorPane 应该在可编辑时显示所有内容,因此它显示注释这一事实并不是一个错误。

还有:

关闭可编辑属性以使 HTML 页面正常呈现(就像在浏览器中一样)。

您现在的选择是:

  • 尝试事先过滤掉所有 HTML 注释或
  • 禁用窗格的可编辑属性,因为这也应该关闭 HTML 注释的呈现
© www.soinside.com 2019 - 2024. All rights reserved.