求助关于用Swing里面的RTFEditorKit读写RTF文档的问题

jgqsyx 2011-11-05 02:22:46
我在用Swing里面提供的RTFEditorKit做读写RTF文档的操作。读写一切正常,但是字体的颜色一直不对。程序如下:
问题1):设置字体颜色是不是用如下程序
StyleConstants.CharacterConstants.setBackground(attrs, Color.red)
问题2):如下程序这么会把从本段开始以后所有的字体颜色都变过来?
StyleConstants.CharacterConstants.setForeground(attrs, Color.red)
问题3):怎么样可以给一段文字加上删除的符号(就是文字中间有一杠)

谢谢各位高手了,小弟没分,不知道能不能解答。谢谢,问题很紧急。

package com.parser.example;

import org.apache.log4j.Logger;

import javax.swing.text.*;
import javax.swing.text.rtf.RTFEditorKit;
import java.awt.*;
import java.io.*;

/**
* User: Administrator
* Date: 2011-11-4
* Time: 23:18:53
*/
public class RTFReader {
private Logger logger = Logger.getLogger(RTFReader.class);

private RTFEditorKit kit;
private static String testRtfFilePath = "E:/test.rtf";

public RTFReader(final RTFEditorKit kit) {
this.kit = kit;
}

public static void main(String args[]) {
RTFEditorKit kit = new RTFEditorKit();
RTFReader utils = new RTFReader(kit);
Document document = utils.buildDocumentByRTF(testRtfFilePath);
SimpleAttributeSet attrs = new SimpleAttributeSet();
StyleConstants.CharacterConstants.setBackground(attrs, Color.red);//字体颜色是不是这样设置的?
StyleConstants.CharacterConstants.setForeground(attrs, Color.red);//如果用setForeground,字体颜色会设置成功,但是之后的颜色都会变成红色。

utils.insertTextToRTF(0, "Test Text", document, attrs);
utils.writeDocumentToRTF(document, testRtfFilePath);
}

public Document insertTextToRTF(int offset, String text, Document document, AttributeSet attributeSet) {
try {
document.insertString(offset, text, attributeSet);
} catch (BadLocationException e) {
logger.error("Something wrong with updating a RTF document");
}
return document;
}

public Document buildDocumentByRTF(String rtfPath) {
File file = new File(rtfPath);
Document document = kit.createDefaultDocument();
InputStream fileInputStream;
try {
fileInputStream = new FileInputStream(file);
kit.read(fileInputStream, document, 0);
fileInputStream.close();
} catch (FileNotFoundException e) {
logger.error("The file does not exist");
} catch (IOException e) {
logger.error("Something wrong with reading the file");
} catch (BadLocationException e) {
logger.error("Something wrong with updating a RTF document");
}
return document;
}

public String getDocumentText(Document document) {
String documentText = null;
try {
documentText = document.getText(0, document.getLength());
} catch (BadLocationException e) {
logger.error("Something wrong with updating a RTF document");
}
return documentText;
}

public File writeDocumentToRTF(Document document, String rtfPath) {
File file = new File(rtfPath);
try {
OutputStream outputStream = new FileOutputStream(file);
kit.write(outputStream, document, 0, document.getLength());
outputStream.flush();
outputStream.close();
} catch (FileNotFoundException e) {
logger.error("The file does not exist");
} catch (IOException e) {
logger.error("Something wrong with reading the file");
} catch (BadLocationException e) {
logger.error("Something wrong with updating a RTF document");
}
return file;
}
}
...全文
175 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
内容概要:本文系统地介绍了基于Matlab/Simulink平台的直流电机双闭环控制系统仿真研究,聚焦于由电流环和转速环构成的双闭环控制结构,详细阐述了PI控制器的设计原理与参数整定方法。通过Simulink搭建完整的电机控制仿真模型,深入分析系统在启动过程、突加负载等典型工况下的动态响应特性,全面验证了双闭环控制策略在提升系统稳定性、响应快速性及抗干扰能力方面的优越性能,为电机控制系统的理论教学、课程实践与工程应用提供了可靠的仿真依据和技术支撑; 适合人群:自动化、电气工程及其相关专业的本科高年级学生、研究生,以及从事电机控制、电力电子与运动控制领域研究的科研人员和工程技术人员; 使用场景及目标:①应用于高校课程设计、毕业设计及科研项目中的电机控制仿真任务;②掌握双闭环PI控制系统的控制机理与Simulink建模与仿真技能;③提升对电机动态性能评估、控制器设计与参数优化的综合实践能力; 阅读建议:建议读者结合Matlab/Simulink软件动手搭建仿真模型,逐步调试PI控制器参数,观察并分析不同参数对系统性能的影响,从而深入理解控制策略的核心作用机制,并可在掌握基础后进一步拓展至先进控制算法如模糊控制、自适应控制和模型预测控制的研究与实现。

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧