java 对象序列化实现复杂UI对象状态的保存。

有时我们在遇到要对复杂对象的状态进行保存时,常常难以处理对象的状态。比如如图所示遇到的情况:

我们要在点击OK按钮时要保存该界面的状态,而在点击Cancel按钮时要取消该次界面的操作,回到之前界面的状态。如果在点击Cancel状态时,该界面的数据状态和初始化一致,则下次我们弹出该对话框时,直接重新初始化该对话框就对了。可是如果之前我们已经对该界面进行了操作。要在点击Cancel后,再次显示该对话框时能回到上次的状态,就比较麻烦了。我们可以对该界面的各个组件的状态进行记录保存,在初始化完该对话框后,根据保存的状态再来填充界面的状态,这能达到我们的要求。可是如果该界面的数据结构比较复杂,这样做就比较麻烦了。下面我们采用java对象序列化对象来实现复杂数据结构状态的保存。

示例

复杂对象代码:

package com.michael.test;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

/**
 *@author mengke
 *@email wqjsir@foxmail.com
 *@version 1.0
 *@date  2011-11-5 下午06:41:39
 */
public class ComplexObject extends JDialog implements Serializable{

	private static final long serialVersionUID = 1L;
    private JButton btnOk;
    private JButton btnCancel;
    private JComboBox comBox;
    private JRadioButton radioBtn;
    private JRadioButton radioBtn2;
    private JCheckBox checkBox;
    private JCheckBox checkBox2;
    private boolean isBtnOkAction;
    
	public ComplexObject(){
		putcompents();
		setSize(new Dimension(200,300));
		setModal(true);
	}

	private void putcompents() {
		comBox = new JComboBox();
		comBox.addItem("item1");
		comBox.addItem("item2");
		
		radioBtn = new JRadioButton("radio button1");
		radioBtn2 = new JRadioButton("radio button2");
		checkBox = new JCheckBox("check box 1");
		checkBox2 = new JCheckBox("check box 2");
		
		JPanel _plBody = new JPanel();
		_plBody.setLayout(new GridLayout(5, 1));
		_plBody.add(comBox);
		_plBody.add(radioBtn);
		_plBody.add(radioBtn2);
		_plBody.add(checkBox);
		_plBody.add(checkBox2);
		
		btnOk = new JButton("OK");
		btnCancel = new JButton("Cancel");
		
		ButtonActionListener _actionListener = new ButtonActionListener();
		btnOk.addActionListener(_actionListener);
		btnCancel.addActionListener(_actionListener);
		
		
		JPanel _plButton = new JPanel();
		_plButton.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
		_plButton.add(btnOk);
		_plButton.add(btnCancel);
		
		setLayout(new BorderLayout());
		add(_plBody,BorderLayout.CENTER);
		add(_plButton,BorderLayout.SOUTH);
	}
	
	public boolean isBtnOkAction(){
		return isBtnOkAction;
	}
	
       /**该类必须实现Serializable接口,不然监听类不会被序列化*/	
    class ButtonActionListener implements ActionListener,Serializable{
		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource() == btnOk){
				isBtnOkAction = true;
				setVisible(false);
			}else if(e.getSource() == btnCancel){
				isBtnOkAction = false;
				setVisible(false);
			}
		}
		
	}
}


 

拥有该复杂对象的类:

package com.michael.test;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * 复杂对象状态的保持
 * 
 * @author mengke
 *@email wqjsir@foxmail.com
 *@version 1.0
 *@date 2011-11-5 下午06:39:12
 */
public class ComplexStatusSave {

	private ComplexObject currentComplexObj;
	private ComplexObject oldComplexObj;
	private boolean isPreSaveOkStatus;

	public void showUI() {
		final JFrame _frame = new JFrame();
		_frame.setLayout(new BorderLayout());
		JButton _btnAction = new JButton("button");
		_btnAction.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (currentComplexObj == null ||oldComplexObj == null|| !isPreSaveOkStatus) {
					currentComplexObj = new ComplexObject( );
					currentComplexObj.setLocationRelativeTo(_frame);
				}
				currentComplexObj.setVisible(true);
				
				//操作前没有保存OK状态,则重新获取是否需要保存OK状态
				if (!isPreSaveOkStatus) {
					//如果点击了OK按钮则保存OK状态
					if(currentComplexObj.isBtnOkAction()){
						oldComplexObj = copyObject(currentComplexObj);
						isPreSaveOkStatus = true;
					}
				}else if(!currentComplexObj.isBtnOkAction()){
					currentComplexObj = oldComplexObj;
					oldComplexObj = copyObject(currentComplexObj);
				} else if (currentComplexObj.isBtnOkAction()) {
					oldComplexObj = copyObject(currentComplexObj);
				}
			}
		});
		JPanel _plButton = new JPanel();
		_plButton.setLayout(new FlowLayout());
		_plButton.add(_btnAction);
		_frame.add(_plButton, BorderLayout.SOUTH);
		_frame.setSize(new Dimension(300, 200));
		_frame.setLocation(300, 200);
		_frame.setVisible(true);
	}

	/**
	 * 对对象进行复制
	 * @param obj
	 * @return
	 */
	private ComplexObject copyObject(Object obj) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try {
			ObjectOutputStream oops = new ObjectOutputStream(baos);
			oops.writeObject(obj);
			oops.close();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}

		ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
		try {
			ObjectInputStream ois = new ObjectInputStream(bais);
			return (ComplexObject) ois.readObject();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String[] args) {
		new ComplexStatusSave().showUI();
	}
}


    对于复杂对象数据状态的保存,这里难就难再有两个按钮,不同的按钮的操作要求有不同的结果。我们这里只是通过java对象序列化来实现对复杂UI对象进行一份拷贝,保存一份历史状态的备份。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java对象序列化和反序列化是通过实现Serializable接口来完成的。下面是实现Java对象序列化和反序列化的步骤: 1. 创建一个实现Serializable接口。例如: ```java import java.io.Serializable; public class MyClass implements Serializable { private int id; private String name; // 省略构造函数和其他方法 // Getter和Setter方法 } ``` 2. 在需要序列化的地方,使用ObjectOutputStream对象写入到文件或流中。例如: ```java MyClass obj = new MyClass(); obj.setId(1); obj.setName("Example"); try { FileOutputStream fileOut = new FileOutputStream("file.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(obj); out.close(); fileOut.close(); System.out.println("对象序列化保存到文件中"); } catch (IOException e) { e.printStackTrace(); } ``` 3. 在需要反序列化的地方,使用ObjectInputStream从文件或流中读取对象。例如: ```java MyClass obj = null; try { FileInputStream fileIn = new FileInputStream("file.ser"); ObjectInputStream in = new ObjectInputStream(fileIn); obj = (MyClass) in.readObject(); in.close(); fileIn.close(); System.out.println("对象已从文件中反序列化"); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } // 使用反序列化后的对象 if (obj != null) { System.out.println("ID: " + obj.getId()); System.out.println("Name: " + obj.getName()); } ``` 以上就是实现Java对象序列化和反序列化的基本步骤。需要注意的是,被序列化必须实现Serializable接口,且的所有非瞬态(transient)成员变量也将被序列化

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值