java-在JEditorPane或JTextPane中设置HTML样式

我想在JEditorPane或JTextPane中显示HTML,但我希望样式(字体大小)来自外观.有没有办法做到这一点,还是必须使用嵌入式HTML样式?

这是一个例子:

epText = new JEditorPane("text/html",content);

StyleSheet ss = ((HTMLEditorKit)epText.getEditorKit()).getStyleSheet();
ss.addRule("p {font-size:" + FontManager.getManager().getFontSize() + "}");
HTMLEditorKit kit = (HTMLEditorKit) epText.getEditorKit();
kit.setStyleSheet(ss);

epText.setEditorKit(kit);

每当我设置编辑器工具包时,所有文本都会消失.我是否需要每次设置文字?

最佳答案
是.

从Java API for HTMLEditorKit深入研究此代码段:

Customization from current LAF

HTML provides a well known set of features without exactly specifying
the display characteristics. Swing has
a theme mechanism for its
look-and-feel implementations. It is
desirable for the look-and-feel to
feed display characteristics into the
HTML views. An user with poor vision
for example would want high contrast
and larger than typical fonts.

The support for this is provided by the StyleSheet class. The
presentation of the HTML can be
heavily influenced by the setting of
the StyleSheet property on the EditorKit.

编辑:设置编辑器工具包时,它会卸载旧工具包并安装新工具包.这改变了基础模型,这就是为什么文本“消失”的原因.
Read the API for more info.

但是您可能不需要重新创建整个套件…只需在样式中添加一个新表即可.

相关文章

vue阻止冒泡事件 阻止点击事件的执行 <div @click=&a...
尝试过使用网友说的API接口获取 找到的都是失效了 暂时就使用...
后台我拿的数据是这样的格式: [ {id:1 , parentId: 0, name:...
JAVA下载文件防重复点击,防止多次下载请求,Cookie方式快速简...
Mip是什么意思以及作用有哪些