怎样用Java代码实现截取的图片放进粘贴板里,
1. // 单击copybtn  按钮
2. public void copybtnClick()
3. {
4. if(targetImg==null){
5. JOptionPane.showMessageDialog(this, "对不起,暂无图片可以复制到粘贴板");
6. return;
7. }8. if(targetImg!=null)
9. {
10. JOptionPane.showMessageDialog(this, "图片已经复制到粘贴板");
11. return;
12. }
13. }并且
java.awt.image.BufferedImage  targetImg 是用来存放截取的图片
望大家给定意见,用什么函数实现?

解决方案 »

  1.   

    http://www.exampledepot.com/egs/java.awt.datatransfer/ToClipImg.html
    // This method writes a image to the system clipboard.
    // otherwise it returns null.
    public static void setClipboard(Image image) {
        ImageSelection imgSel = new ImageSelection(image);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imgSel, null);
    }// This class is used to hold an image while on the clipboard.
    public static class ImageSelection implements Transferable {
        private Image image;    public ImageSelection(Image image) {
            this.image = image;
        }    // Returns supported flavors
        public DataFlavor[] getTransferDataFlavors() {
            return new DataFlavor[]{DataFlavor.imageFlavor};
        }    // Returns true if flavor is supported
        public boolean isDataFlavorSupported(DataFlavor flavor) {
            return DataFlavor.imageFlavor.equals(flavor);
        }    // Returns image
        public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
            if (!DataFlavor.imageFlavor.equals(flavor)) {
                throw new UnsupportedFlavorException(flavor);
            }
            return image;
        }
    }