设置文本到剪切板
1 public void setIntoClipboard(String data) { 2 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); 3 clipboard.setContents(new StringSelection(data), null); 4 }
获取剪切板文本
1 public String getFromClipboard() { 2 Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); 3 if (transferable != null && transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) { 4 try { 5 return (String) transferable.getTransferData(DataFlavor.stringFlavor); 6 } catch (Exception e) { 7 return ""; 8 } 9 } 10 return ""; 11 }