gpt4 book ai didi

java - JTextPane,文本/html 内容 : nested HTML element do not inherit font size?

转载 作者:行者123 更新时间:2023-11-29 04:36:40 24 4
gpt4 key购买 nike

我有这段代码来显示 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);

但这是输出:

enter image description here

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

我该如何解决这个问题?

最佳答案

绝对是一个错误。您可以通过在 CSS 中添加显式继承来解决它,使用 <style>元素:

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>");

关于java - JTextPane,文本/html 内容 : nested HTML element do not inherit font size?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41144139/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com