1. 往剪切板写文本数据Java代码
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.StringSelection;
public void actionPerformed(ActionEvent e) {
clipboard = getToolkit().getSystemClipboard(); //获得系统粘贴板
StringSelection textInfoSelected = new StringSelection(textInfo.getText()); //建立一个粘贴板内容实例.
clipboard.setContents(textInfoSelected, null); //将textInfoSelected加入到粘贴板中;
labelInfo.setText("The output message has copied to clipboard.");
}
protected static String getClipboardText(Clipboard clip) throws Exception{
// 获取剪切板中的内容
Transferable clipT = clip.getContents(null);
if (clipT != null) {
// 检查内容是否是文本类型
if (clipT.isDataFlavorSupported(DataFlavor.stringFlavor))
return (String)clipT.getTransferData(DataFlavor.stringFlavor);
}
return null;
}