众所周知,这个包主要是与系统剪切板相关的的软件包(还有就是与JFC里的拖曳编程相关),这个包的核心类当然是Clipborad类(个人认为),这个 类我们就看成是一个剪切板,它具有储存的内容,我们可以取得和设置它的内容.它的内容被封装在Transferable里,我们最终要取得的数据,就是在 它里面.说到Transferable接口,我不得不提一下StringSelection类,这个类就是它的一个实现,专门用来向剪切板传输文本数据 的.为什么要叫StringSelection,我想因为平时我们把文本复制到剪切板的时候,这些文本都是被选择好的,所以就会取个这个类名吧.呵 呵~~~当我们用StringSelection向Clipborad传输文本数据的时候,我们剪切板的Transferable数据就可以是一个 StringSelection对象.像这样:
StringSelection ss=new StringSelection("放入剪切板的文本");
clipboardobj.setContents(ss,ss);//两个参数都是ss,因为StringSelection既是Transferable,也是ClipboardOwner.
DataFlavor表示的是存储数据的格式,因为我们的剪切板里到底是文本,图像,还是上次复制的文件我们根本不知道,幸好有它让我们可以先知道Clipboard里存的是些什么东西.
一个剪切板还可以加入数据改变的监听器,当剪切板的数据改变时做一些操作.这主要是在Clipboard里加入监听即可.
package cn.sisy.awt; import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.UnsupportedFlavorException; import java.io.IOException; public class CopyStringToClipboard { public static void main(String[] args) { String str = "a string for copy"; Toolkit toolkit = Toolkit.getDefaultToolkit(); Clipboard clipboard = toolkit.getSystemClipboard(); StringSelection strSel = new StringSelection(str); clipboard.setContents(strSel, null); if(clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)){ try { String content = (String) clipboard.getData(DataFlavor.stringFlavor); System.out.println(content); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
已复制如下:
下面看看图片如何复制:
实现自定义的ImageSelection类用于支持图片的复制:
package cn.sisy.awt; import java.awt.*; import java.awt.datatransfer.*; import java.awt.datatransfer.UnsupportedFlavorException; import java.io.IOException; public class ImageSelection implements Transferable { private Image image; public ImageSelection(Image image) { this.image = image; } @Override public DataFlavor[] getTransferDataFlavors() { return new DataFlavor[]{DataFlavor.imageFlavor}; } @Override public boolean isDataFlavorSupported(DataFlavor flavor) { // TODO Auto-generated method stub return flavor.equals(DataFlavor.imageFlavor); } @Override public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { if(flavor.equals(DataFlavor.imageFlavor)){ return image; }else{ throw new UnsupportedFlavorException(flavor); } } }
实现类CopyImage
package cn.sisy.awt; import java.awt.*; import java.awt.datatransfer.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.List; public class CopyImage { private Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); private final int AREA_WIDTH = 400; private final int AREA_HEIGHT = 400; private int preX = -1; private int preY = -1; BufferedImage image = new BufferedImage(AREA_WIDTH, AREA_HEIGHT, BufferedImage.TYPE_INT_RGB); java.util.List<Image> imageList = new java.util.ArrayList<Image>(); Graphics g = image.getGraphics(); private Frame f = new Frame("图形复制程序"); private MyCanvas myCanvas = new MyCanvas(); private Color foreColor = new Color(255,0,0); private Button copy = new Button("复制"); private Button paste = new Button("粘贴"); public void init(){ g.fillRect(0, 0, AREA_WIDTH, AREA_HEIGHT); myCanvas.setPreferredSize(new Dimension(AREA_WIDTH, AREA_HEIGHT)); myCanvas.addMouseMotionListener(new MouseAdapter() { public void mouseDragged(MouseEvent e){ preX = e.getX(); preY = e.getY(); if(preX > 0 && preY > 0){ g.setColor(foreColor); g.drawLine(preX,preY,e.getX(),e.getY()); } myCanvas.repaint(); } }); myCanvas.addMouseListener(new MouseAdapter(){ public void mouseReleased(MouseEvent e){ preX = -1; preY = -1; } }); f.add(myCanvas); Panel p = new Panel(); copy.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { ImageSelection contents = new ImageSelection(image); clipboard.setContents(contents, null); } }); paste.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(clipboard.isDataFlavorAvailable(DataFlavor.imageFlavor)){ try { imageList.add((Image)clipboard.getData(DataFlavor.imageFlavor)); myCanvas.repaint(); } catch (Exception e1) { e1.printStackTrace(); } } } }); p.add(copy); p.add(paste); f.add(p,BorderLayout.SOUTH); f.pack(); f.setVisible(true); } public static void main(String[] args) { new CopyImage().init(); } class MyCanvas extends Canvas{ @Override public void paint(Graphics g) { g.drawImage(image,0,0,null); System.out.println(imageList.size()); for(Image img : imageList){ g.drawImage(img,0,0,null); } } } }