我写的写字板,文本编辑区用的JTextPane,实现了插入图片,以及单独设置选中部分字体,但不知怎么保存打开。目前只能做到像系统自带的记事本一样(字体的个体属性存不下来)。请赐教。试了试写序列化,照猫画虎的写下来还是不行。贴上我的部分代码,万谢啦 :) 很着急啊!!斜线注释了的部分是只能像记事本那样保存打开的代码,其他的是我照猫画虎写的序列化。 void SaveAssist(File fl)《--保存用到的函数
{
try
{
// RTFEditorKit rtfEditor = new RTFEditorKit();
// TextArea.setEditorKit(rtfEditor);
FileOutputStream fos = null;
fos = new FileOutputStream(fl);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(sd);
oos.flush();
oos.close();
}
catch(IOException ioe)
{
System.err.println("IOException: " + ioe.getMessage());
}
// FileOutputStream fos = null;
// String text = TextArea.getText();
// try
// {
// fos = new FileOutputStream(fl);
// }
// catch(FileNotFoundException fnfe)
// {
// System.err.println(fnfe);
// return;
// }
// catch(IOException ioe)
// {
// System.err.println(ioe);
// }
// finally
// {
// try
// {
// if (fos != null)
// fos.close();
// }
// catch(IOException ioe){}
// }
saved = true;
setTitle(fl.getName()+" - 文档");
File_Save.setEnabled(false);
Tool_Save.setEnabled(false);
} void OpenAssist()《--打开用到的函数
{
JFileChooser fc = new JFileChooser();
FileFilterr f1 = new FileFilterr(".txt","文本文档(*.txt)");
FileFilterr f2 = new FileFilterr(".doc","Word 文档(*.doc)");
fc.addChoosableFileFilter(f1);
fc.addChoosableFileFilter(f2);
if(fc.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
{
File file = new File(fc.getCurrentDirectory(), fc.getName());
if(file.exists())
{
// try
// {
// BufferedReader br = new BufferedReader(new FileReader(file));
// String string;
// while ((string = br.readLine())!=null)
// TextArea.setText(string);
// br.close();
// TextArea.setCaretPosition(0);
// }
// catch(IOException ioe)
// {
// System.err.println(ioe);
// }
// }
// else
// {
// JOptionPane.showMessageDialog(null,fc.getSelectedFile().getName() + "\n找不到文件\n请检查所给的文件名是否正确","打开",0);
// }
FileInputStream fis = null;
try
{
// RTFEditorKit rtfEditor = new RTFEditorKit();
// TextArea.setEditorKit(rtfEditor);
fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
TextArea.setStyledDocument((StyledDocument)ois.readObject());
ois.close();
validate();
}
catch (IOException ioe)
{
System.err.println("IOException: " + ioe.getMessage());
}
catch (ClassNotFoundException cnf)
{
System.err.println("Class not found: " + cnf.getMessage());
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if(fis!=null)
fis.close();
if(fc.getChoosableFileFilters()!=null)
{
fc.removeChoosableFileFilter(f1);
fc.removeChoosableFileFilter(f2);
}
}
catch(IOException ie){}
}
}
}
}
{
try
{
// RTFEditorKit rtfEditor = new RTFEditorKit();
// TextArea.setEditorKit(rtfEditor);
FileOutputStream fos = null;
fos = new FileOutputStream(fl);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(sd);
oos.flush();
oos.close();
}
catch(IOException ioe)
{
System.err.println("IOException: " + ioe.getMessage());
}
// FileOutputStream fos = null;
// String text = TextArea.getText();
// try
// {
// fos = new FileOutputStream(fl);
// }
// catch(FileNotFoundException fnfe)
// {
// System.err.println(fnfe);
// return;
// }
// catch(IOException ioe)
// {
// System.err.println(ioe);
// }
// finally
// {
// try
// {
// if (fos != null)
// fos.close();
// }
// catch(IOException ioe){}
// }
saved = true;
setTitle(fl.getName()+" - 文档");
File_Save.setEnabled(false);
Tool_Save.setEnabled(false);
} void OpenAssist()《--打开用到的函数
{
JFileChooser fc = new JFileChooser();
FileFilterr f1 = new FileFilterr(".txt","文本文档(*.txt)");
FileFilterr f2 = new FileFilterr(".doc","Word 文档(*.doc)");
fc.addChoosableFileFilter(f1);
fc.addChoosableFileFilter(f2);
if(fc.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
{
File file = new File(fc.getCurrentDirectory(), fc.getName());
if(file.exists())
{
// try
// {
// BufferedReader br = new BufferedReader(new FileReader(file));
// String string;
// while ((string = br.readLine())!=null)
// TextArea.setText(string);
// br.close();
// TextArea.setCaretPosition(0);
// }
// catch(IOException ioe)
// {
// System.err.println(ioe);
// }
// }
// else
// {
// JOptionPane.showMessageDialog(null,fc.getSelectedFile().getName() + "\n找不到文件\n请检查所给的文件名是否正确","打开",0);
// }
FileInputStream fis = null;
try
{
// RTFEditorKit rtfEditor = new RTFEditorKit();
// TextArea.setEditorKit(rtfEditor);
fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
TextArea.setStyledDocument((StyledDocument)ois.readObject());
ois.close();
validate();
}
catch (IOException ioe)
{
System.err.println("IOException: " + ioe.getMessage());
}
catch (ClassNotFoundException cnf)
{
System.err.println("Class not found: " + cnf.getMessage());
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if(fis!=null)
fis.close();
if(fc.getChoosableFileFilters()!=null)
{
fc.removeChoosableFileFilter(f1);
fc.removeChoosableFileFilter(f2);
}
}
catch(IOException ie){}
}
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货