Java中如何操作剪贴板
2008-11-30 22:32
323 查看
import java.awt.*;
import java.awt.datatransfer.*;
import java.io.*;
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);
Toolkit.getDefaultToolkit().getSystemClipboard().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;
}
}
}
这段代码主要描述了以文字和图片两种形式读写剪贴板操作。
import java.awt.datatransfer.*;
import java.io.*;
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);
Toolkit.getDefaultToolkit().getSystemClipboard().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;
}
}
}
这段代码主要描述了以文字和图片两种形式读写剪贴板操作。
相关文章推荐
- 【java源码】如何用java代码实现对excel的操作
- Java如何读取和操作上G文本数据
- Java8 Lambda表达式和流操作如何让你的代码变慢5倍
- Services开源框架之CXF与在java程序中如何实现操作cmd执行网络命令
- Java 如何对文件进行多个Object对象流的读写操作
- Java如何操作Word, Excel, PDF文档
- Java利用happen-before规则如何实现共享变量的同步操作详解
- Java如何操作Android的adb shell 之 我自己在程序中的使用方法
- 3-1 数组; 如何使用java中的数组; 使用循环操作 Java 中的数组
- 计算机二级考试java软件操作教程 教大家如何学习java
- java 如何操作 Cookie
- Java中如何向系统剪贴板写入富文本
- Java如何操作Word, Excel, PDF文档(2)
- 在Java 7里如何对文件进行操作
- Java中如何实现每天定时对数据库的操作
- 如何用Java操作MongoDB(1)
- 若想在当前路径下去运行非当前路径下的.class文件(即java可执行文件),该如何操作?
- Android ADB 请问怎么操作手机剪贴板,如何读取和写入剪贴板内容?
- 如何用Java操作MongoDB?
- 如何使用Java操作LDAP之检索条目(二)