JAVA操作粘贴板

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

public class Test {
	//这段代码主要描述了以文字和图片两种形式读写剪贴板操作。
	public static void main(String[] args) {
		String s = "abc";
		setClipboard(s);
		System.out.println(getClipboard());

	}

	// If a string is on the system clipboard, this method returns it;
	// otherwise it returns null.
	//从粘贴板得到字符
	public static String getClipboard() {
		Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard()
				.getContents(null);//从系统剪切板中获取数据  

		try {
			if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {//判断是否为文本类型  
				String text = (String) t
						.getTransferData(DataFlavor.stringFlavor);//从数据中获取文本值
				return text;
			}
		} catch (UnsupportedFlavorException e) {
		} catch (IOException e) {
		}
		return null;
	}

	// This method writes a string to the system clipboard.
	// otherwise it returns null.
	//把字符写进粘贴板
	public static void setClipboard(String str) {
		StringSelection ss = new StringSelection(str); //构建String数据类型  
		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();//获取系统剪切板
		clipboard.setContents(ss, null);//添加文本到系统剪切板  
	}

	// deal with image
	// Getting and Setting an Image on the System Clipboard
	// If an image is on the system clipboard, this method returns it;
	// otherwise it returns null.
	//从粘贴板得到图片
	public static Image getImageClipboard() {
		Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard()
				.getContents(null);

		try {
			if (t != null && t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
				Image image = (Image) t.getTransferData(DataFlavor.imageFlavor);
				return image;
			}
		} catch (UnsupportedFlavorException e) {
		} catch (IOException e) {
		}
		return null;
	}

	// Setting an image on the system clipboard requires a custom Transferable
	// object to hold the image while on the clipboard.
	// This method writes a image to the system clipboard.
	// otherwise it returns null.
	//把图片写到粘贴板
	public static void setImageClipboard(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;
		}
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值