参考文章
关键代码
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Clipboard类的2个主要方法:
- setContents(Transferable contents, ClipboardOwner owner)
- getContents(Object requestor)
Clipboard类:实现使用剪切/复制/粘贴操作传输数据的机制的类
代码示例(仅供参考)
package exer04; 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 CopyToClipboard{ public static void main(String[] args) throws Exception{ Transferable canonicalNameSelection = new StringSelection("java"); Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); //实现复制功能:将当前程序中的字符串复制到剪贴板中去 // clipboard.setContents(canonicalNameSelection, null); canonicalNameSelection = clipboard.getContents(null); //获取剪贴板上的内容 Object obj = canonicalNameSelection.getTransferData(DataFlavor.stringFlavor); System.out.println(obj); } }