import java.io.*;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
public class Test {
static JEditorPane editor = new JEditorPane();
public static void main(String agrs[]){
JFrame frame = new JFrame();
JButton write = new JButton("write");
JButton read = new JButton("read");
read.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
File file = new File("test.tt");
try{
ObjectInputStream in = new ObjectInputStream(
new FileInputStream(file));
editor = (JEditorPane)in.readObject();
System.out.println(editor.getText());
}catch(Exception ex){
System.out.println("has a error!");
}
}
});
write.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
File file = new File("test.tt");
try{
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream(file));
out.writeObject(editor);
}catch(FileNotFoundException ex){
System.out.println("has a FileNotFoundException!");
}catch(IOException ex){
ex.printStackTrace();
System.out.println("has a IOException!");
}
}
});
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(editor,BorderLayout.CENTER);
frame.getContentPane().add(read,BorderLayout.NORTH);
frame.getContentPane().add(write,BorderLayout.SOUTH);
frame.setSize(500,400);
frame.setVisible(true);
}
}
如果我没有设置内容类型他就出现这个错误:
java.io.NotSerializableException: javax.swing.text.GapContent$StickyPosition
如果我设置了以下两种就出现下面的错误:
editor.setContentType("UTF-8");
editor.setContentType("ISO-8859-1");
java.io.NotSerializableException: java.lang.Object
都把我搞晕了,到底哪里有问题啊?