在 jbutton 中序列化一个对象

Serializing an Object in a jbutton

我在 netbean 中用 jframe 制作了一个 jbutton,我想在单击 jbutton 时序列化一个对象

错误:java.io.NotSerializableException:javax.swing.GroupLayout

我没有序列化任何布局,但为什么会出现此错误?

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
getValue1 a =new getValue1();
a.name=jTextField1.getText();
a.ps=jTextField2.getText();
a.type=jComboBox1.getSelectedItem().toString();

try{
   FileOutputStream fileOut =new FileOutputStream("C:\employee.ser");
   ObjectOutputStream out = new ObjectOutputStream(fileOut);
   out.writeObject(a);
   out.close();
   fileOut.close();
   System.out.printf("C:\employee.ser");
}
catch(IOException i){
        i.printStackTrace();
    }
}

public class getValue1 implements java.io.Serializable{
    public String name;
    public String ps;
    public String type;
}

如果 getValue1(对于 class 来说是一个错误的名称,classes 不是字段,名称应该更像 Value)是另一个 [=14= 的声明的一部分](这是一个内部 class)序列化程序也会尝试序列化父 class。

您应该将 class 移动到新的 class 声明中。