java serializable 源码_copyserializable.java

import java.awt.*;import java.awt.event.*;import java.awt.datatransfer.*;import java.io.*;/** * Description: * Copyright (C), 2005-2008, Yeeku.H.Lee * This program is protected by copyright laws. * Program Name: * Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */public class CopySerializable{Frame f = new Frame("复制对象");Button copy = new Button("复制");Button paste = new Button("粘贴");TextField name = new TextField(15);TextField age = new TextField(15);TextArea ta = new TextArea(3 , 30);//创建系统剪贴板Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();public void init(){Panel p = new Panel();p.add(new Label("姓名"));p.add(name);p.add(new Label("年龄"));p.add(age);f.add(p , BorderLayout.NORTH);f.add(ta);Panel bp = new Panel();copy.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){copyPerson();}});paste.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){try{readPerson();}catch (Exception ee){ee.printStackTrace();}}});bp.add(copy);bp.add(paste);f.add(bp , BorderLayout.SOUTH);f.pack();f.setVisible(true);}public void copyPerson(){Dog d = new Dog(name.getText() , Integer.parseInt(age.getText()));SerialSelection ls = new SerialSelection(d);clipboard.setContents(ls , null);}public void readPerson()throws Exception{DataFlavor peronFlavor = new DataFlavor("application/x-java-serialized-object;class=Dog");if (clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)){Dog d = (Dog)clipboard.getData(peronFlavor);ta.setText(d.toString());}}public static void main(String[] args) {new CopySerializable().init();}}class Dog implements Serializable {private String name;private int age;public Dog(){}public Dog(String name , int age){this.name = name;this.age = age;}public void setName(String name){this.name = name;}public String getName(){ return this.name;}public void setAge(int age){this.age = age;}public int getAge(){ return this.age;}public String toString(){return "Dog对象[ name=" + name + " , age=" + age + " ]";}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值