多个对象复制到剪贴板

huangapple go评论109阅读模式
英文:

Multiple objects to clipboard

问题

我的问题是,我想要复制一些文本一张图片到系统剪贴板。我尝试过使用awt和javafx,但没有找到方法来实现。

我尝试过使用awt的方法来处理单个图片。不确定如何添加多个内容。

  1. MyTransferableImage image = new MyTransferableImage();
  2. Toolkit.getDefaultToolkit().getSystemClipboard().setContents(image, this);

以及javafx的方法:

  1. Clipboard clipboard = Clipboard.getSystemClipboard();
  2. ClipboardContent content = new ClipboardContent();
  3. content.putImage(myImage);
  4. // 编辑
  5. clipboard.setContent(content);

但是都无法在剪贴板上存储多个元素。

英文:

My problem is that I would like to copy some text and an image to the system's clipboard. I have tried with awt, and javafx but have not found the way to do it.

I've tried the awt solution for a single image. Not sure how to add multiple content.

  1. MyTransferableImage image = new MyTransferableImage();
  2. Toolkit.getDefaultToolkit().getSystemClipboard().setContents(image, this);

and the javafx solution:

  1. Clipboard clipboard = Clipboard.getSystemClipboard();
  2. ClipboardContent content = new ClipboardContent();
  3. content.putImage(myImage);
  4. // edited
  5. clipboard.setContent(content);

But neither was able to store multiple elements on clipboard.

答案1

得分: 3

你没有将创建的内容设置到ClipBoard

  1. final Clipboard clipboard = Clipboard.getSystemClipboard();
  2. final ClipboardContent content = new ClipboardContent();
  3. // 添加你的元素
  4. content.putString("文本");
  5. content.putImage(new Image("https://www.oracle.com/a/tech/img/cb88-java-logo-001.jpg"));
  6. // 设置剪贴板的内容
  7. clipboard.setContent(content);

参考:Clipboard : Oracle 帮助中心

英文:

You are not setting the created content to the ClipBoard :

  1. final Clipboard clipboard = Clipboard.getSystemClipboard();
  2. final ClipboardContent content = new ClipboardContent();
  3. // Add your elements
  4. content.putString("Text");
  5. content.putImage(new Image("https://www.oracle.com/a/tech/img/cb88-java-logo-001.jpg"));
  6. // Set the Clipboard's content
  7. clipboard.setContent(content);

ref: Clipboard : Oracle Help Center

huangapple
  • 本文由 发表于 2020年3月16日 17:33:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/60703412.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定