设定foreground代码如下:
SimpleAttributeSet textColour = new SimpleAttributeSet();
StyleConstants.setForeground(textColour, Colors.RED);
textPane.setCharacterAttributes(textColour, false);
可以正常导出正确的HTML文本,但是直接修改setForeground为setBackground,是无法导出正确的HTML文本的。
workaround的原链接在此:
https://stackoverflow.com/questions/13285526/jtextpane-text-background-color-does-not-work
以上代码修改为:
String htmlStyle = "background-color:"+ getHTMLColor(Colors.RED);
SimpleAttributeSet textColour = new SimpleAttributeSet();
textColour.addAttribute(HTML.Attribute.STYLE, htmlStyle);
MutableAttributeSet outerAttr = new SimpleAttributeSet();
outerAttr.addAttribute(HTML.Tag.SPAN, textColour);
StyleConstants.setBackground(outerAttr, backgroundColors.getSelectedItem().c);
StyleConstants.setBackground(textColour, backgroundColors.getSelectedItem().c);
textPane.setCharacterAttributes(outerAttr, false);
textPane.setCharacterAttributes(textColour, false);
其中:
public static String getHTMLColor(Color color) {
if (color == null) {
return "#000000";
}
return "#" + Integer.toHexString(color.getRGB()).substring(2).toUpperCase();
}
导出的HTML增加了一节:
<span style="background-color:#FF0000">
解决此问题
解决JTextPane设定其Background颜色无法导出正确的HTML的问题
设定foreground代码如下:
SimpleAttributeSet textColour = new SimpleAttributeSet();
StyleConstants.setForeground(textColour, Colors.RED);
textPane.setCharacterAttributes(textColo...
- 发表于 2018-09-17 21:48
- 阅读 ( 293 )
- 分类:网络文章
你可能感兴趣的文章
- Java 添加条码、二维码到PDF文档 134 浏览
- Java 在Excel中添加筛选器并执行筛选 91 浏览
- JAVA + VUE + 简洁UI:分离式软件平台形成之旅 265 浏览
- Html特殊字符大全 136 浏览
- Java 设置Word中的表格自适应的3种方式 113 浏览
- LeaRun快速开发平台,.net/java项目开发工具简析 142 浏览
随机文章
- MySQL中查询、删除重复记录的方法大全 4212 浏览
- JAVA判断两个时间戳,相隔多少分钟 4632 浏览
- 工具类:Java操作字节流工具类 2622 浏览
- 微信小程序识别菊花码的生成过程 2640 浏览
- 微信小程序识别菊花码的识别 3338 浏览
相关问题
- java基础知识提问 1 回答
条评论
请先 登录 后评论
发送私信
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!