java.awt.datatransfer.Clipboard剪切板复制粘贴String
java.awt.Toolkit
是一个可用来获取操作剪切板的工具
Toolkit toolkit = Toolkit.getDefaultToolkit();
获得 tookit
Clipboard systemClipboard = toolkit.getSystemClipboard();
获得操作系统级的剪切板
Toolkit toolkit = Toolkit.getDefaultToolkit(); //tookit可用来获得系统剪切板
//操作系统的系统剪切板
Clipboard systemClipboard = toolkit.getSystemClipboard();
//虚拟机的剪切板只能在虚拟机范围起作用
//Clipboard jvmClipboard = new Clipboard("虚拟机自身的剪切板");String 想要要复制的字符串 = "想要复制的字符串";
StringSelection stringSection = new StringSelection(想要要复制的字符串);//setContents(Transferable contents,ClipOwner owner); 将剪切板的内容设置到指定的Transferable对象,并将指定的剪切板所有者作为新内容的所有者注册
systemClipboard.setContents(stringSection, null);System.out.println("文本内容以复制到剪切板, 可以打开一个文本编辑器,粘贴,看看");System.out.println("让后执行从剪切板取出内容并输出到控制台的操作,从剪切板获取到的内容为:👇");// @param requestor the object requesting the clip data (not used)
// getContents(的参数), 指请求数据的请求者, 无用, 可以填null
Transferable transferable = systemClipboard.getContents(null);String str = (String) transferable.getTransferData(DataFlavor.stringFlavor);System.out.println(str);
package 剪切板复制粘贴;import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;public class AwtClipboard剪切板复制粘贴String230528 {public static void main(String...arugments)throws Exception{Toolkit toolkit = Toolkit.getDefaultToolkit(); //tookit可用来获得系统剪切板//操作系统的系统剪切板Clipboard systemClipboard = toolkit.getSystemClipboard();//虚拟机的剪切板只能在虚拟机范围起作用//Clipboard jvmClipboard = new Clipboard("虚拟机自身的剪切板");String 想要要复制的字符串 = "想要复制的字符串";StringSelection stringSection = new StringSelection(想要要复制的字符串);//StringSelection实现了Transferable接口, 所以也可写为👇// Transferable stringSection = new StringSelection(想要要复制的字符串);//setContents(Transferable contents,ClipOwner owner); 将剪切板的内容设置到指定的Transferable对象,并将指定的剪切板所有者作为新内容的所有者注册systemClipboard.setContents(stringSection, null);System.out.println("文本内容以复制到剪切板, 可以打开一个文本编辑器,粘贴,看看");System.out.println("让后执行从剪切板取出内容并输出到控制台的操作,从剪切板获取到的内容为:👇");// @param requestor the object requesting the clip data (not used)// getContents(的参数), 指请求数据的请求者, 无用, 可以填nullTransferable transferable = systemClipboard.getContents(null);String str = (String) transferable.getTransferData(DataFlavor.stringFlavor);System.out.println(str);}}
包装,文本到系统剪切板的方法
public static Clipboard strToSystemClipboard(String 要放入剪切板的字符串, Toolkit tookit) {Clipboard systemClipboard = null;if(tookit==null)tookit=Toolkit.getDefaultToolkit();systemClipboard=tookit.getSystemClipboard();Transferable transferable = new StringSelection(要放入剪切板的字符串);systemClipboard.setContents(transferable, null);return systemClipboard;}public static Clipboard strToSystemClipboard(String 要放入剪切板的字符串) {return strToSystemClipboard(要放入剪切板的字符串, null);}
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;public class AwtClipboard文本到剪切板的方法 {public static Clipboard strToSystemClipboard(String 要放入剪切板的字符串, Toolkit tookit) {Clipboard systemClipboard = null;if(tookit==null)tookit=Toolkit.getDefaultToolkit();systemClipboard=tookit.getSystemClipboard();Transferable transferable = new StringSelection(要放入剪切板的字符串);systemClipboard.setContents(transferable, null);return systemClipboard;}public static Clipboard strToSystemClipboard(String 要放入剪切板的字符串) {return strToSystemClipboard(要放入剪切板的字符串, null);}public static void main(String...arguments) {strToSystemClipboard("在文本编辑器粘贴,看看内容是否到了系统剪切板");}}
对比操作系统剪切板和虚拟机剪切板
package 剪切板复制粘贴;import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;public class AwtClipboard对比操作系统Clipboard和虚拟机Clipboard {public static void main(String...arguments)throws Exception{Toolkit tookit = Toolkit.getDefaultToolkit();Clipboard systemClipboard = tookit.getSystemClipboard();Clipboard jvmClipboard = new Clipboard("javaClipboard");StringSelection transfer010 = new StringSelection("要复制粘贴的内容一");Transferable transfer020 = new StringSelection("要复制粘贴的内容二");systemClipboard.setContents(transfer010, null);jvmClipboard.setContents(transfer020, null);System.out.println("可以打开一个文本编辑器,Ctrl+v或右键粘贴看看内容");System.out.println("操作系统 剪切板的内容是: \t" + systemClipboard.getContents(null).getTransferData(DataFlavor.stringFlavor));System.out.println("Java虚拟机剪切板的内容是: \t"+ jvmClipboard.getContents(null).getTransferData(DataFlavor.stringFlavor));}}
java.awt.datatransfer.Clipboard剪切板获取String字符串文本
有两种方法获取
-
直接从Clipboard获得
(String) systemClipboard.getData(DataFlavor.stringFlavor);
-
从Clipboard获得Transable再获得String
(String) systemClipboard.getContents(null).getTransferData(DataFlavor.stringFlavor);
Clipboard 的 getData(DataFlavor flavor)
的源码, java1.5版开始才有
public Object getData(DataFlavor flavor)throws UnsupportedFlavorException, IOException {if (flavor == null) {throw new NullPointerException("flavor");}Transferable cntnts = getContents(null);if (cntnts == null) {throw new UnsupportedFlavorException(flavor);}return cntnts.getTransferData(flavor);}
包装的获取剪切板文本的方法,没有判断内容是否是String
public static String getSystemClipboardString(Clipboard systemClipboard) {String result = null;if(systemClipboard==null)systemClipboard=Toolkit.getDefaultToolkit().getSystemClipboard();try {// result = (String) systemClipboard.getContents(null).getTransferData(DataFlavor.stringFlavor);// ↑都可↓ 下面的getData方法1.5版才有result = (String) systemClipboard.getData(DataFlavor.stringFlavor);} catch (UnsupportedFlavorException | IOException e) {e.printStackTrace();}return result;}public static String getSystemClipboardString() {return getSystemClipboardString(null);}
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;public class AwtClipboard获取剪切板文本的方法 {public static String getSystemClipboardString(Clipboard systemClipboard) {String result = null;if(systemClipboard==null)systemClipboard=Toolkit.getDefaultToolkit().getSystemClipboard();try {// result = (String) systemClipboard.getContents(null).getTransferData(DataFlavor.stringFlavor);// ↑都可↓ 下面的getData方法1.5版才有result = (String) systemClipboard.getData(DataFlavor.stringFlavor);} catch (UnsupportedFlavorException | IOException e) {e.printStackTrace();}return result;}public static String getSystemClipboardString() {return getSystemClipboardString(null);}public static void main(String...arguments) {System.out.println(getSystemClipboardString());}}
判断剪切板Transferable内容是否是String
- 方法1, 从clipboard获得transferable, 再用transferable的
isDataFlavorSupported(DataFlavor flavor)
方法判断
boolean 是否剪切板的内容是文本 = transferable.isDataFlavorSupported(DataFlavor.stringFlavor);
- 方法2 直接用clipboard的
isDataFlavorAvailable(DataFlavor flavor)
方法判断 , 该方法1.5版才有
boolean 是否剪切板的内容是文本 = systemClipboard.isDataFlavorAvailable(DataFlavor.stringFlavor);
Clipboard的isDataFlavorAvailable(DataFlavor flavor)
方法从1.5版本开始才有, 可以简化代码, 其实质也是获得Transferable实例后调用其isDataFlavorSupported(DataFlavor flavor)
方法判断
源码如下
/*** Returns whether or not the current contents of this clipboard can be* provided in the specified {@code DataFlavor}.** @param flavor the requested {@code DataFlavor} for the contents* @return {@code true} if the current contents of this clipboard can be* provided in the specified {@code DataFlavor}; {@code false}* otherwise* @throws NullPointerException if {@code flavor} is {@code null}* @throws IllegalStateException if this clipboard is currently unavailable* @since 1.5*/public boolean isDataFlavorAvailable(DataFlavor flavor) {if (flavor == null) {throw new NullPointerException("flavor");}Transferable cntnts = getContents(null);if (cntnts == null) {return false;}return cntnts.isDataFlavorSupported(flavor);}
包装的获取剪切板文本的方法,且判断内容是否是String
public static String getSystemClipboardString(Clipboard systemClipboard) {String result = "剪切板的内容不是字符串文本StringText";if(systemClipboard==null)systemClipboard=Toolkit.getDefaultToolkit().getSystemClipboard();// boolean 剪切板的内容是文本 = systemClipboard.getContents(null).isDataFlavorSupported(DataFlavor.stringFlavor);boolean 剪切板的内容是文本 = systemClipboard.isDataFlavorAvailable(DataFlavor.stringFlavor); // 1.5版才有if(剪切板的内容是文本) {try {// result = (String) systemClipboard.getContents(null).getTransferData(DataFlavor.stringFlavor);result = (String) systemClipboard.getData(DataFlavor.stringFlavor); // 1.5版才有} catch (UnsupportedFlavorException | IOException e) {e.printStackTrace();}}return result;}public static String getSystemClipboardString() {return getSystemClipboardString(null);}
package 剪切板复制粘贴;import java.awt.Toolkit;
import java.awt.datatransfer.*;
import java.io.IOException;public class 判断剪切板的内容是文本才获取 {public static String getSystemClipboardString(Clipboard systemClipboard) {String result = "剪切板的内容不是字符串文本StringText";if(systemClipboard==null)systemClipboard=Toolkit.getDefaultToolkit().getSystemClipboard();// boolean 剪切板的内容是文本 = systemClipboard.getContents(null).isDataFlavorSupported(DataFlavor.stringFlavor);boolean 剪切板的内容是文本 = systemClipboard.isDataFlavorAvailable(DataFlavor.stringFlavor); // 1.5版才有if(剪切板的内容是文本) {try {// result = (String) systemClipboard.getContents(null).getTransferData(DataFlavor.stringFlavor);result = (String) systemClipboard.getData(DataFlavor.stringFlavor); // 1.5版才有} catch (UnsupportedFlavorException | IOException e) {e.printStackTrace();}}return result;}public static String getSystemClipboardString() {return getSystemClipboardString(null);}public static void main (String...arguments)throws Exception{System.out.println(getSystemClipboardString());}}