java实现复制内容到剪贴板

时间:2022-06-08 23:55:58
package com.yang.clipboard;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;

public class ClipBoardUtil {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        setSysClipboardText("复制的内容");
    }
    /** * 将字符串复制到剪切板。 */  
    public static void setSysClipboardText(String writeMe) {  
        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();  
        Transferable tText = new StringSelection(writeMe);  
        clip.setContents(tText, null);  
    } 
}