恢复先前序列化的JFrame对象,如何? [英] Restore previously serialized JFrame-object, how?

查看:100
本文介绍了恢复先前序列化的JFrame对象,如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设法序列化了一个非常基本的GUI对象,其中包含一个JTextArea和一些按钮到文件"test.ser".

I have managed to serialize my very basic GUI-object containing a JTextArea and a few buttons to a file 'test.ser'.

现在,我想从'test.ser'中完全恢复以前保存的状态,但是似乎对如何正确地反序列化对象状态有误解.

Now, I would like to completely restore the previously saved state from 'test.ser', but seem to have a misconception of how to properly deserialize an objects state.

MyFrame 创建JFrame并对其进行序列化.

The class MyFrame creates the JFrame and serializes it.

public class MyFrame extends JFrame implements ActionListener {


 // Fields
 JTextArea textArea;
 String title;
 static MyFrame gui = new MyFrame();
 private static final long serialVersionUID = 1125762532137824262L;


 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  gui.run();
 }

 // parameterless default contructor
 public MyFrame() {

 }

 // constructor with title
 public MyFrame(String title) {

 }

 // creates Frame and its Layout
 public void run() {

  JFrame frame = new JFrame(title);
  JPanel panel_01 = new JPanel();
  JPanel panel_02 = new JPanel();

  JTextArea textArea = new JTextArea(20, 22);
  textArea.setLineWrap(true);

  JScrollPane scrollPane = new JScrollPane(textArea);

  scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);

  panel_01.add(scrollPane);



  // Buttons
  JButton saveButton = new JButton("Save");
  saveButton.addActionListener(this);
  JButton loadButton = new JButton("Load");
  loadButton.addActionListener(this);


  panel_02.add(loadButton);
  panel_02.add(saveButton);
  // Layout
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.getContentPane().add(BorderLayout.CENTER, panel_01);
  frame.getContentPane().add(BorderLayout.SOUTH, panel_02);

  frame.setSize(300, 400);
  frame.setVisible(true);
 }

 /*
  * 
  */
 public void serialize() {

  try {
   ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test.ser"));
   oos.writeObject(gui);
   oos.close();
  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
  }
 }


 public void actionPerformed(ActionEvent ev) {
  System.out.println("Action received!");
  gui.serialize();
 }

}

在这里我尝试进行反序列化:

Here I try to do the deserialization:

public class Deserialize {
 static Deserialize ds;
 static MyFrame frame;



 public static void main(String[] args) {
  try {
   ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.ser"));
    frame = (MyFrame) ois.readObject();
    ois.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

也许有人可以指出我误解的方向?

Maybe somebody could point me into the direction where my misconception is?

你们将如何编写一个类,该类反序列化并将先前序列化的GUI元素恢复为其先前序列化的状态?

How would you guys write a class, which deserializes and restores the previously serialized gui-elements to their previously serialized state?

我现在的操作方式似乎在概念上有多个缺陷,对吗?

The way I am doing it right now seems to have more than one flaw in its concept, right?

推荐答案

会发生什么?你有例外吗?从代码的外观来看,从未初始化 ds .我相信,一旦反序列化,您将需要使用 frame.setVisible(true); 显示框架.与往常一样,Swing(实际上是AWT)应仅在AWT事件调度线程(EDT)上使用-使用 java.awt.EventQueue.invokeLater .

What happens? Are you getting an exception? From the looks of the code ds is never initialised. I believe, once deserialised, you will need to show the frame with frame.setVisible(true);. As always, Swing (and in fact AWT) should be used only on the AWT Event Dispatch Thread (EDT) - use java.awt.EventQueue.invokeLater.

通常使用静态方法不是一个好主意.也不对GUI组件进行序列化. final 是好的,并且在大多数情况下,请确保实例和静态字段已初始化.

Generally using statics is not a good idea. Nor is serialising GUI components. final is good, and will, for the most part, make sure instance and static fields are initialised.

这篇关于恢复先前序列化的JFrame对象,如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆