欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java 复制文本到剪贴板,java剪贴板,在java awt中使用

来源: javaer 分享于  点击 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();    }}
相关栏目:

用户点评