【问题标题】:JTextPane, text/html content: nested HTML element do not inherit font size?JTextPane,文本/html 内容:嵌套的 HTML 元素不继承字体大小?
【发布时间】:2017-04-29 20:58:37
【问题描述】:

我有这个代码来显示HTML格式化字符串:

JTextPane pane = new JTextPane();
        pane.setContentType("text/html");
        pane.setEditable(false);
        pane.setText(
            "<html>" +
                "<body style='font-size:18px'>" +
                    "<h1>Error:</h1>" +
                    "<p>" +
                        "Could not read the file <code>none.txt</code>. " +
                        "Perhaps it does not exist in the specified location, " +
                        "or possibly there is no access to it" +
                    "</p>" +
                "</body>" +
            "</html>");
        add(pane);

但这是输出:

您可以看到none.txt 字符串没有继承其封闭段落的字体大小,尽管这是在 HTML (see jsfiddle) 中应该发生的情况。

我该如何解决这个问题?

【问题讨论】:

  • 也许在 标签之前结束body标签,然后在之后重新声明body标签?示例:“无法读取文件

标签: java html swing jtextpane


【解决方案1】:

绝对是一个错误。您可以通过在 CSS 中使用 &lt;style&gt; 元素添加显式继承来解决此问题:

pane.setText(
    "<html>" +
        "<style>\ncode { font-size: inherit; }\n</style>" +
        "<body style='font-size:18px'>" +
            "<h1>Error:</h1>" +
            "<p>" +
                "Could not read the file <code>none.txt</code>. " +
                "Perhaps it does not exist in the specified location, " +
                "or possibly there is no access to it" +
            "</p>" +
        "</body>" +
    "</html>");

【讨论】:

  • 这很烦人。现在我必须为我可能使用的每一种块添加inheritboldcode,等等...?
  • 我认为&lt;b&gt; 不需要它。可能只是改变字体系列的那些。 &lt;code&gt;&lt;pre&gt;&lt;samp&gt;,可能还有&lt;kbd&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-07
  • 2017-09-12
  • 1970-01-01
  • 1970-01-01
  • 2021-11-07
  • 2013-10-16
  • 1970-01-01
相关资源
最近更新 更多