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