java 复制文本到剪贴板,java剪贴板,在java awt中使用
分享于 点击 47567 次 点评:211
java 复制文本到剪贴板,java剪贴板,在java awt中使用
在java awt中使用Clipboard类控制剪贴板,将文本复制到剪贴板很容易,如下代码实现:
package cn.outofmemory.example;import java.awt.Toolkit;import java.awt.datatransfer.Clipboard;import java.awt.datatransfer.StringSelection;/** * * @author byrx.net */public class Main { /** * Places text on the clipboard */ public void placeTextOnClipboard() { //Get the toolkit Toolkit toolkit = Toolkit.getDefaultToolkit(); //Get the clipboard Clipboard clipboard = toolkit.getSystemClipboard(); //The setContents method of the Clipboard instance takes a Transferable //as first parameter. The StringSelection class implements the Transferable //interface. StringSelection stringSel = new StringSelection("text to be placed on the clipboard"); //We specify null as the clipboard owner clipboard.setContents(stringSel, null); } /** * @param args the command line arguments */ public static void main(String[] args) { new Main().placeTextOnClipboard(); }}
用户点评