ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-12-10 06:00:24  阅读:372  来源: 互联网

标签:jeditorpane swing html java


我想在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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有