Java 如何将textPane中的StyledDocument保存到.doc文件中?
我不知道这是否可行,但我想做的是将一个样式化文档(用户可以更改文本:粗体、下划线、斜体和3种字体大小)保存在.doc文件中,这样他以后就可以用任何其他支持样式化文本的文本编辑器自己打开它 我写了下面的代码。。。编辑器可以工作,我可以在文本上应用样式,但当我保存时,它会将文本保存为黑色,没有样式。我想不出问题出在哪里。也许这些行动并不能拯救我们。我尝试了writer和缓冲writer,但都不起作用。我还尝试使用HTML编辑器工具包,但它根本不起作用——它保存了一个空白文档 也许有人知道如何保存样式?感谢您的帮助:)Java 如何将textPane中的StyledDocument保存到.doc文件中?,java,swing,jtextpane,doc,styleddocument,Java,Swing,Jtextpane,Doc,Styleddocument,我不知道这是否可行,但我想做的是将一个样式化文档(用户可以更改文本:粗体、下划线、斜体和3种字体大小)保存在.doc文件中,这样他以后就可以用任何其他支持样式化文本的文本编辑器自己打开它 我写了下面的代码。。。编辑器可以工作,我可以在文本上应用样式,但当我保存时,它会将文本保存为黑色,没有样式。我想不出问题出在哪里。也许这些行动并不能拯救我们。我尝试了writer和缓冲writer,但都不起作用。我还尝试使用HTML编辑器工具包,但它根本不起作用——它保存了一个空白文档 也许有人知道如何保存样式
公共类EditFrame扩展了javax.swing.JFrame{
JFrame frameEdit=this;
File File;//我想保存到->fileName.doc的文件
样式文件;
HashMap操作;
StyledEditorKit;
公共编辑框架(){
超级();
初始化组件();
JMenu editMenu=createEditMenu();
}
受保护的JMenu createEditMenu(){
JMenu菜单=编辑菜单;
Action Action=new StyledEditorKit.BoldAction();
action.putValue(action.NAME,“粗体”);
菜单。添加(操作);
action=newstylededitorkit.italication();
action.putValue(action.NAME,“斜体”);
菜单。添加(操作);
//...
返回菜单;
}
//我猜这也不正确(不阅读样式),但这是另一个主题:)
公共无效读取文件(文件f){
试一试{
BufferedReader=新的BufferedReader(新的InputStreamReader(新文件InputStream(f),“windows-1250”);
textPane.read(读卡器,空);
textPane.requestFocus();
}捕获(IOEX异常){
Logger.getLogger(EditFrame.class.getName()).log(Level.SEVERE,null,ex);
}
}
//保存方法
私有无效保存(java.awt.event.ActionEvent evt){
试一试{
BufferedOutputStream out=新的BufferedOutputStream(新文件输出流(文件));
kit=(StyledEditorKit)textPane.getEditorKit();
doc=(StyledDocument)textPane.getDocument();
kit.write(out,doc,0,doc.getLength());
}捕获(FileNotFoundException ex){
Logger.getLogger(EditFrame.class.getName()).log(Level.SEVERE,null,ex);
}捕获(不支持编码异常| BadLocationException ex){
Logger.getLogger(EditFrame.class.getName()).log(Level.SEVERE,null,ex);
}捕获(IOEX异常){
Logger.getLogger(EditFrame.class.getName()).log(Level.SEVERE,null,ex);
}
}
公共静态void main(字符串参数[]){
invokeLater(new Runnable()){
公开募捐{
新建EditFrame().setVisible(true);
}
});
}
}
您可以使用,它支持(RTF)。许多文字处理器,包括微软word,都可以使用这种格式。坚持使用OutputStream
,它写入“适合这种内容处理程序的格式”。另一个使用Writer
的将“作为纯文本写入”给定流
为什么StyledEditorKit
不起作用
从“将文本视为纯文本”获取其write()
实现。StyledEditorKit
在内部存储样式化文本,但不知道任何外部格式。您必须转到其中一个子类,HTMLEditorKit
或RTFEditorKit
,以获取覆盖默认write()
的内容。重写的方法知道如何将内部格式转换为外部格式,如RTF。您可以使用,它支持(RTF)。许多文字处理器,包括微软word,都可以使用这种格式。坚持使用OutputStream
,它写入“适合这种内容处理程序的格式”。另一个使用Writer
的将“作为纯文本写入”给定流
为什么StyledEditorKit
不起作用
从“将文本视为纯文本”获取其
write()
实现。StyledEditorKit
在内部存储样式化文本,但不知道任何外部格式。您必须转到其中一个子类,HTMLEditorKit
或RTFEditorKit
,以获取覆盖默认write()
的内容。重写的方法知道如何将内部格式转换为外部格式,如RTF。除了使用不同的(在本例中为RTF)格式之外,真的没有其他方法吗?为什么StyledEditorKit不起作用?我打赌我只是没有以正确的方式保存某些东西……我认为这就是它的设计方式。我试着在上面解释。卡塔琳娜岛谢谢你的解释。。我将首先尝试使用HTML,如果我有任何问题,请告诉您。除了使用不同的(在本例中为RTF)格式之外,真的没有其他方法吗?为什么StyledEditorKit不起作用?我打赌我只是没有以正确的方式保存某些东西……我认为这就是它的设计方式。我试着在上面解释。卡塔琳娜岛谢谢你的解释。。我会先尝试使用HTML,如果我有任何问题,会告诉你。
public class EditFrame extends javax.swing.JFrame {
JFrame frameEdit = this;
File file; //A file I would like to save to -> fileName.doc
StyledDocument doc;
HashMap<Object, Action> actions;
StyledEditorKit kit;
public EditFrame() {
super();
initComponents();
JMenu editMenu = createEditMenu();
}
protected JMenu createEditMenu() {
JMenu menu = editMenu;
Action action = new StyledEditorKit.BoldAction();
action.putValue(Action.NAME, "Bold");
menu.add(action);
action = new StyledEditorKit.ItalicAction();
action.putValue(Action.NAME, "Italic");
menu.add(action);
//...
return menu;
}
//I'm guessing this doesn't work correctly too (doesn't read styles), but this is another subject :)
public void readFile(File f) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), "windows-1250"));
textPane.read(reader, null);
textPane.requestFocus();
} catch (IOException ex) {
Logger.getLogger(EditFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
//SAVE METHOD
private void save(java.awt.event.ActionEvent evt) {
try {
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
kit = (StyledEditorKit) textPane.getEditorKit();
doc = (StyledDocument) textPane.getDocument();
kit.write(out, doc, 0, doc.getLength());
} catch (FileNotFoundException ex) {
Logger.getLogger(EditFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedEncodingException | BadLocationException ex) {
Logger.getLogger(EditFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(EditFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new EditFrame().setVisible(true);
}
});
}
}