编程语言
首页 > 编程语言> > 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.

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

标签:jeditorpane,swing,html,java
来源: https://codeday.me/bug/20191210/2099810.html