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

前端之家收集整理的这篇文章主要介绍了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.

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

猜你在找的HTML相关文章