在 Java 中复制到剪贴板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3591945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 02:18:30  来源:igfitidea点击:

Copying to the clipboard in Java

javastringclipboardawt

提问by clone1018

I want to set the user's clipboard to a string in a Java console application. Any ideas?

我想在 Java 控制台应用程序中将用户的剪贴板设置为字符串。有任何想法吗?

回答by user85421

Use the Toolkitto get the system clipboard. Create a StringSelectionwith the Stringand add it to the Clipboard.

使用Toolkit来获取系统剪贴板。创建一个StringSelectionString并把它添加到Clipboard

Simplified:

简化:

StringSelection selection = new StringSelection(theString);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(selection, selection);

回答by camickr

Here is a simple SSCCEto accomplish this:

这是一个简单的SSCCE来完成这个:

import java.awt.*;
import java.awt.datatransfer.*;
import java.io.*;

class ClipboardTest
{
    public static void main(String[] args)
        throws UnsupportedFlavorException, IOException
    {
        Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
        StringSelection testData;

        //  Add some test data

        if (args.length > 0)
            testData = new StringSelection( args[0] );
        else
            testData = new StringSelection( "Test Data" );

        c.setContents(testData, testData);

        //  Get clipboard contents, as a String

        Transferable t = c.getContents( null );

        if ( t.isDataFlavorSupported(DataFlavor.stringFlavor) )
        {
            Object o = t.getTransferData( DataFlavor.stringFlavor );
            String data = (String)t.getTransferData( DataFlavor.stringFlavor );
            System.out.println( "Clipboard contents: " + data );
        }

        System.exit(0);
    }
}

回答by rado

If you are on Linux and using OpenJDK, it will not work. You must use the Sun JDK on Linux for it to work.

如果您在 Linux 上使用 OpenJDK,它将无法工作。您必须在 Linux 上使用 Sun JDK 才能使其工作。

回答by user1819551

This work like a champ;

这项工作就像冠军一样;

 String get = text.getText();
 StringSelection selec = new StringSelection(get);
 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
 clipboard.setContents(selec, selec);

回答by Iovanny Olguín ávila

In Linux with xclip:

在带有 xclip 的 Linux 中:

Runtime run = Runtime.getRuntime();
Process p = null;
String str = "hello";
try {
        p = run.exec(new String[]{"sh", "-c", "echo " + str + " | xclip -selection clipboard"});
}
catch (Exception e) {
    System.out.println(e);
}

回答by Xerus

For anyone still stumbling upon this post searching for the JavaFXway to accomplish this, here you go:

对于仍然在这篇文章中寻找JavaFX方式来实现这一目标的人来说,这里是:

ClipboardContent content = new ClipboardContent();
content.putString("Some text");
content.putHtml("<b>Bold</b> text");
Clipboard.getSystemClipboard().setContent(content);

For further information, read the documentation.

有关更多信息,请阅读文档