java.io.NotSerializableException: java.beans.EventHandler

java.io.NotSerializableException: java.beans.EventHandler

我有这个异常我不明白为什么会抛出它或者我应该如何处理它。

        try{
            File file = chooser.getSelectedFile();
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(frame);
            oos.writeObject(list);
            oos.close();
        }catch (IOException e){
            JOptionPane.showMessageDialog(null, e);
        }

我相信在读取 oos.writeObject(frame) 时会抛出异常。在这里,我试图将 JFrame 对象保存为二进制文件。以前我能够用 XMLEncoder.

类似地将同一个对象写入 XML
 import java.awt.*;
 import java.awt.event.*;
 import java.beans.*;
 import java.io.*;
 import java.util.ArrayList;

 import javax.swing.*;


 @SuppressWarnings("serial")
 public class SaveFrame implements Serializable{
public static JFileChooser chooser;
public JFrame frame;
public JTextArea textArea;
public ArrayList<String> list = new ArrayList<String>();
public static void main(String[] args){
    chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File("."));      
    SaveFrame test = new SaveFrame();
    test.initialize();
}

public void initialize(){

    frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(800, 300);
    frame.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

    JButton loadXML = new JButton("LoadXML");
    frame.getContentPane().add(loadXML);
    loadXML.addActionListener(EventHandler.create(ActionListener.class, this, "loadXML"));

    JButton saveXML = new JButton("SaveXML");
    frame.getContentPane().add(saveXML);
    saveXML.addActionListener(EventHandler.create(ActionListener.class, this, "saveXML"));

    JButton loadBinary = new JButton("LoadBinary");
    frame.getContentPane().add(loadBinary);
    loadBinary.addActionListener(EventHandler.create(ActionListener.class, this, "loadBinary"));

    JButton saveBinary = new JButton("SaveBinary");
    frame.getContentPane().add(saveBinary);
    saveBinary.addActionListener(EventHandler.create(ActionListener.class, this, "saveBinary"));

    JButton storeData = new JButton("StoreData");
    frame.getContentPane().add(storeData);
    storeData.addActionListener(EventHandler.create(ActionListener.class, this, "storeData"));

    JButton printData = new JButton("PrintData");
    frame.getContentPane().add(printData);

    textArea = new JTextArea(30,30);
    frame.getContentPane().add(textArea);
    printData.addActionListener(EventHandler.create(ActionListener.class, this, "printData"));


    frame.setVisible(true);
}

public void loadXML(){
    int r = chooser.showOpenDialog(null);

    if(r == JFileChooser.APPROVE_OPTION){
        try{
            File file = chooser.getSelectedFile();
            XMLDecoder decoder = new XMLDecoder(new FileInputStream(file));
            decoder.readObject();
            decoder.close();
        }
        catch (IOException e){
            JOptionPane.showMessageDialog(null, e);
        }
    }
}

public void saveXML(){
    if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION){
        try{
            File file = chooser.getSelectedFile();
            XMLEncoder encoder = new XMLEncoder(new FileOutputStream(file));
            encoder.writeObject(frame);
            encoder.writeObject(list);
            encoder.close();
        }
        catch (IOException e){
            JOptionPane.showMessageDialog(null, e);
        }
    }
}
public void storeData(){
    list.add(textArea.getText());
}
public void printData(){
    for(String s : list){
        textArea.append(s + " ");
    }
}
public void loadBinary() throws ClassNotFoundException{
    int r = chooser.showOpenDialog(null);

    if(r == JFileChooser.APPROVE_OPTION){
        try{
            File file = chooser.getSelectedFile();
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
            ois.readObject();
            ois.close();
        }
        catch (IOException e){
            JOptionPane.showMessageDialog(null, e);
        }
    }
}
public void saveBinary(){
    if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION){
        try{
            File file = chooser.getSelectedFile();
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(frame);
            oos.writeObject(list);
            oos.close();
        }
        catch (IOException e){
            JOptionPane.showMessageDialog(null, e);
             }
          }
      } 
 }

我不太明白为什么当我保存到 XML 而不是二进制文件格式时它会起作用。我从 documentation of ObjectOutputStream and it's examples would lead me to believe that this should work as XMLEncoder 中读到的所有内容都是如此。我会为您打印出我的堆栈,但这是一个图形用户界面,我不知道如何在控制台中启用它。

根据您documentation指出的,

Only objects that support the java.io.Serializable interface can be written to streams.

并且根据 Java Doc of java.bean.EventHandler,它没有实现 Serializable

我不知道您可能一直在阅读哪些内容会让您有所期待。

您链接到的非常 ObjectOutputStream Javadoc 具体说明了

Only objects that support the java.io.Serializable interface can be written to streams.

它需要传递给它的对象以及通过非静态非瞬态引用从中可以访问的所有对象来实现Serializable

XMLEncoder 没有那个限制。