Java swing组件的串行化方法

开发 后端
本文介绍Java swing组件的串行化方法,使用ObjectInputStream读取文件中的对象和使用ObjectOutputStream把对象写入文件。

由于JButton和JTree都已经实现了Serializable接口,因此Java swing组件的串行化和读取是可以做到的。Java swing组件串行化方法就是使用ObjectInputStream读取文件中的对象,使用ObjectOutputStream把对象写入文件。

如:

  1. import java.io.FileInputStream;  
  2. import java.io.FileNotFoundException;  
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.ObjectInputStream;  
  6. import java.io.ObjectOutputStream;  
  7.  
  8. import javax.swing.JButton;  
  9. import javax.swing.JTree;  
  10.  
  11. public class Save {  
  12.  
  13. public static void main(String[] args) {  
  14.  
  15. // Write  
  16. JButton button = new JButton("TEST Button");  
  17. JTree tree = new JTree();  
  18. try {  
  19. ObjectOutputStream outForButton = new ObjectOutputStream(  
  20. new FileOutputStream("button"));  
  21. outForButton.writeObject(button);  
  22. outForButton.close();  
  23. ObjectOutputStream outForTree = new ObjectOutputStream(  
  24. new FileOutputStream("tree"));  
  25. outForTree.writeObject(tree);  
  26. outForTree.close();  
  27. } catch (FileNotFoundException e) {  
  28. e.printStackTrace();  
  29. } catch (IOException e) {  
  30. e.printStackTrace();  
  31. }  
  32. // Read  
  33.  
  34. try {  
  35. ObjectInputStream inForButton = new ObjectInputStream(  
  36. new FileInputStream("button"));  
  37. JButton buttonReaded = (JButton) inForButton.readObject();  
  38.  
  39. ObjectInputStream inForTree = new ObjectInputStream(  
  40. new FileInputStream("tree"));  
  41. JTree treeReaded = (JTree) inForTree.readObject();  
  42. } catch (FileNotFoundException e) {  
  43. e.printStackTrace();  
  44. } catch (IOException e) {  
  45. e.printStackTrace();  
  46. } catch (ClassNotFoundException e) {  
  47. // TODO Auto-generated catch block  
  48. e.printStackTrace();  
  49. }  
  50.  
  51. }  
  52.  

以上是Java swing组件的串行化和读取是可以做到的,一点小小的总结,希望能对大家有所帮助。

【编辑推荐】

  1. 动态语言是否将会挽救Swing
  2. 用TableModel框架简化Swing开发
  3. Swing模型过滤技术详解
  4. Java Swing开发的一些热点
  5. 使用Swing动态界面设计技术透析
责任编辑:佚名 来源: IT专家网
相关推荐

2009-06-09 16:14:47

Java swing组件串行化

2009-11-18 11:05:27

PHP串行化

2016-11-17 22:18:31

id串行化服务器

2009-09-11 12:17:59

C#控件属性

2009-11-02 16:41:55

VB.NET串行化对象

2019-03-25 07:39:35

ID串行化消息顺序性高可用

2010-01-12 10:29:51

VB.NET对象串行化

2010-01-06 10:49:54

PHP串行化JSON

2021-04-14 15:01:44

串行化方式缓存

2009-11-17 16:24:27

PHP变量串行化

2010-01-14 18:00:07

VB.NET串行化对象

2009-07-17 10:31:18

paint方法Swing组件

2010-01-06 10:58:06

建立JavaScrip

2012-01-17 13:16:34

JavaSwing

2009-07-15 11:02:32

Swing组件

2009-07-14 17:21:42

Swing组件

2009-07-10 16:29:32

Swing组件

2010-01-06 11:05:35

JSON

2009-07-15 13:06:38

Swing组件

2009-07-10 18:06:59

JTree Swing
点赞
收藏

51CTO技术栈公众号