我正在编写一段代码,其中我需要扩展JPanel,然后序列化它的结果。然而,在尝试序列化它时,我得到了这个错误:
java.io.NotSerializableException: javax.swing.GroupLayout我尝试序列化的对象的代码如下:
public class NewJPanel extends javax.swing.JPanel
{
/**
* Creates new form NewJPanel
*/
public NewJPanel()
{
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents()
{
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration
}尝试序列化一个普通的、非扩展的JPanel是可行的,但是扩展的a不行。正如您所看到的,没有使用GroupLayout,面板默认使用FlowLayout。即使显式设置它也不能阻止这一点。谁能告诉我为什么会发生这种情况,以及我可以如何修复或解决此问题?
发布于 2018-11-27 02:11:49
原来,当我将JPannel的布局管理器放到JFrame中时,Netbeans上的JPannel表单构建器偷偷地将其设置为GroupLayout,而不是将其保留为我在JPannel的代码中指定的布局。
https://stackoverflow.com/questions/53471688
复制相似问题