您现在的位置是:首页 >

swing界面 Java swing组件的串行化和读取

火烧 2022-04-19 18:05:41 1043
Java wi g组件的串行化和读取   由于JButto 和JTree都已经实现了Serializa le接口 因此Java wi g组件的串行化和读取是可以做到的 方法就是使用O jectI ut

Java swing组件的串行化和读取  

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

  

  import java io FileInputStream;import java io FileNotFoundException;import java io FileOutputStream;import java io IOException;import java io ObjectInputStream;import java io ObjectOutputStream;

  import javax swing JButton;import javax swing JTree;

  public class Save {

  public static void main(String[] args) {

  // WriteJButton button = new JButton( TEST Button );JTree tree = new JTree();try {ObjectOutputStream outForButton = new ObjectOutputStream(new FileOutputStream( button ));outForButton writeObject(button);outForButton close();ObjectOutputStream outForTree = new ObjectOutputStream(new FileOutputStream( tree ));outForTree writeObject(tree);outForTree close();} catch (FileNotFoundException e) {e printStackTrace();} catch (IOException e) {e printStackTrace();}// Read

  try {ObjectInputStream inForButton = new ObjectInputStream(new FileInputStream( button ));JButton buttonReaded = (JButton) inForButton readObject();

  ObjectInputStream inForTree = new ObjectInputStream(new FileInputStream( tree ));JTree treeReaded = (JTree) inForTree readObject();} catch (FileNotFoundException e) {e printStackTrace();} catch (IOException e) {e printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto generated catch blocke printStackTrace();}

  }

  }

swing界面 Java swing组件的串行化和读取
lishixinzhi/Article/program/Java/hx/201311/25601  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码