java 复制图片到剪切板

java 如何复制图片到剪切板呢?

下面是我做的一个软件:
java 复制图片到剪切板_第1张图片
项目源代码见附件 

这里面就有"复制图片到剪切板"功能.核心方法如下:

/***
	 * 复制图片到剪切板
	 * @param image
	 */
	public static void setClipboardImage(JFrame frame, final Image image) {
		Transferable trans = new Transferable() {
			@Override
			public Object getTransferData(DataFlavor flavor)
					throws UnsupportedFlavorException, IOException {
				if (isDataFlavorSupported(flavor)) {
					return image;
				}
				throw new UnsupportedFlavorException(flavor);
			}

			@Override
			public DataFlavor[] getTransferDataFlavors() {
				return new DataFlavor[] { DataFlavor.imageFlavor };
			}

			@Override
			public boolean isDataFlavorSupported(DataFlavor flavor) {
				return DataFlavor.imageFlavor.equals(flavor);
			}
		};

		frame.getToolkit().getSystemClipboard().setContents(trans, null);
	}

 

在JFrame中如何设置图标

方式一:

URL url=this.getClass().getResource("/com/kingbase/img/kingbase_log.PNG");
//        System.out.println(url.getPath());
        ImageIcon icon = new ImageIcon(url);
        frame.setIconImage(icon.getImage());

 

方式二:

InputStream is = this.getClass().getResourceAsStream(
            "/com/kingbase/img/kingbase_log.PNG");
        BufferedInputStream isr = new BufferedInputStream(is);
        byte[] b = null;
        try
        {
            b = new byte[isr.available()];
            isr.read(b);
            System.out.println(b.length);
        }
        catch (IOException e2)
        {
            e2.printStackTrace();
        }
        if (b != null)
        {
            ImageIcon imageIcon = new ImageIcon(b);
            frame.setIconImage(imageIcon.getImage());
        }

 

方式三:

InputStream is = this.getClass().getClassLoader().getResourceAsStream(
            "com/kingbase/img/kingbase_log.PNG");前面没有斜杠
        BufferedInputStream isr = new BufferedInputStream(is);
        byte[] b = null;
        try
        {
            b = new byte[isr.available()];
            isr.read(b);
            System.out.println(b.length);
        }
        catch (IOException e2)
        {
            e2.printStackTrace();
        }
        if (b != null)
        {
            ImageIcon imageIcon = new ImageIcon(b);
            frame.setIconImage(imageIcon.getImage());
        }

 

 

你可能感兴趣的:(java)