e638. 向剪切板获取和粘贴图像
2024-10-18 15:35:19
// If an image is on the system clipboard, this method returns it;
// otherwise it returns null.
public static Image getClipboard() {
Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); try {
if (t != null && t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
Image text = (Image)t.getTransferData(DataFlavor.imageFlavor);
return text;
}
} 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 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;
}
}
Related Examples |
最新文章
- emoji表情引发的JNI崩溃
- SQLServer学习笔记<;>;sql的范围内查找,sql数据类型,字符串处理函数
- Xcode中的几个常用文件路径
- 获得servletContext的路径的方法
- XML学习总结
- Android学习第一课
- 新版本的tlplayer for android ,TigerLeapMC for windows发布了
- C与C++的区别
- 20170422早会训话,ps:程序出现两次BUG,领导很生气
- utc时间转成local时间
- 写入多线程Log4net 多线程写入
- leveldb 学习记录(一) skiplist
- Git与GitHub学习笔记(四)合并远程分支
- p1654 OSU!
- FoxMail提示:请求的名称有效,但是找不到请求的类型的数据
- java怎样获得JVM可能的总内存,最大内存,以及空暇内存?
- Mysql 中创建索引和索引的使用问题
- 最新PHPcms9.6.0 任意文件上传漏洞
- 《Programming with Objective-C》第五章 Customizing Existing Classes
- Python学习札记(十七) 高级特性3 列表生成式