简体   繁体   English

JFrame 的序列化移除了 JButton 的 ActionListener - Java

[英]Serialization of JFrame removes JButton's ActionListener - Java

I'm relatively new to serialization, so I created a test application to mess around with it.我对序列化比较陌生,所以我创建了一个测试应用程序来处理它。 I serialized a JFrame which had 3 added JComponents : a JTextArea and two JButtons .我序列化了一个JFrame ,其中添加了 3 个JComponents :一个JTextArea和两个JButtons One button saved the JFrame 's state (ie serialization) and the other loaded the serialized JFrame (ie deserialization).一个按钮保存JFrame的状态(即序列化),另一个按钮加载序列化的 JFrame(即反序列化)。

When I typed a message in the JTextArea , clicked the button to save the state, and then loaded the JFrame from the SER file, I got the exact same object (typed message and all) except that the buttons no longer worked.当我在JTextArea键入消息,单击按钮以保存状态,然后从 SER 文件加载JFrame ,我得到了完全相同的对象(键入的消息和所有),只是按钮不再起作用。

Would someone mind explaining why this behaved the way it did?有人会介意解释为什么会这样吗?

PS I know that it's generally not a good practice to serialize GUI components, but this is for learning purposes only. PS我知道序列化GUI组件通常不是一个好习惯,但这仅用于学习目的。

我让实现ActionListener的类也实现了Serializable ,这解决了这个问题。

Yes that would definitely help ie making the class implementing ActionListener implement Serializable. 是的,那肯定会有所帮助,即使实现ActionListener的类实现Serializable。 The question here is why does the process not throw a NotSerializableException, seeing the component has an event listener which is not serializable.Why does the listener be simply excluded from serialization rather than an Exception being thrown. 这里的问题是为什么看到组件具有不可序列化的事件侦听器,进程为什么不引发NotSerializableException,为什么将侦听器简单地从序列化中排除,而不是抛出异常? Also, what should I do if Im using lambda expressions for implementing ActionListener? 另外,如果我使用lambda表达式实现ActionListener怎么办? Please help😭 请帮忙😭

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM