设定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">
解决此问题
分享到:
相关推荐
JAVA-----JTextPane实例 代码
JTextPane使用,可以传图片,文字等
NULL 博文链接:https://icarusliu.iteye.com/blog/1412007
SWT JTextPane 可以把控件中需要的字符串的颜色修改成自己想要的颜色
演示、说明程序 博文链接:https://bps.iteye.com/blog/136627
本源码实现了 文本区里加图片和插入艺术字的功能 窗口使用Windows窗体风格
NULL 博文链接:https://icarusliu.iteye.com/blog/2098986
NULL 博文链接:https://gh0769.iteye.com/blog/598839
课程设计期间做的Java的桌面端小程序,可以发送Emoji表情的聊天室,另外在文件夹中放置了自己的关键代码部分解析,关于emoji部分可查看笔者博客...
java写的记事本程序带工具栏可以改变字体颜色,neatbeans 6.5以上可以直接运行,附带实验报告以及源码。实现了打开,保存,退出,复制,粘贴,剪切,改变字体颜色等基本功能。
1、(BufferedReader)读文字的速度非常慢~~~~~10M左右的东西要30秒到1分钟,如何提高文字读取的速度——一部分一部分的读也行 ...怎么现在用JTextPane标记的文字的颜色消失了? yujinlong0001@sina.com
JTextPane jtpShow; //显示文件 JLabel jlInfo; //提示信息 public HTTPBrowserDemo() { super("韩公达制作的html浏览器"); //调用父类构造函数 jtfAddress=new JTextField(20); //实例化地址输入框 jbGo...
Java代码编写的简单网页浏览器,仿IE基础功能,只能解析标准的HTML文件,现在流行的HTML5格式不能解析。使用方法:可直接输入文件名或网络地址,但必需事先连入网络。... //输出出错信息 } } }
JTextPane jtpShow; //显示页面 JTextArea jtaSource; //显示HTML源文件 public HTTPBrowser(){ super("HTTP连接与浏览"); //调用父类构造函数 jtfAddress=new JTextField(30); //实例化地址输入框 ...
用JTextPanel实现文本带下划线功能