我正在开发 JavaFX 应用程序。我想使用上下文菜单从应用程序复制图像,并使用 Windows 的粘贴功能粘贴它。
File file = new File("C:\\Users\\Admin\\Desktop\\my\\mysql.gif");
Image image = new Image(file.toURI().toString());
ImageView ive =new ImageView(image);
cm = new ContextMenu();
MenuItem copy = new MenuItem("Copy");
cm.getItems().add(copy);
copy.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
//Paste Image at location
Clipboard clipboard = Clipboard.getSystemClipboard();
ClipboardContent content = new ClipboardContent();
content.putImage(image); // the image you want, as javafx.scene.image.Image
clipboard.setContent(content);
}
});
例如,如下图所示。
并且想要通过使用 Windows 功能菜单粘贴到某个位置。
最佳答案
使用Clipboard
和 ClipboardContent
,例如如:
Clipboard clipboard = Clipboard.getSystemClipboard();
ClipboardContent content = new ClipboardContent();
// for paste as image, e.g. in GIMP
content.putImage(image); // the image you want, as javafx.scene.image.Image
// for paste as file, e.g. in Windows Explorer
content.putFiles(java.util.Collections.singletonList(new File("C:\\Users\\Admin\\Desktop\\my\\mysql.gif")));
clipboard.setContent(content);
要使 Windows 上下文菜单的“粘贴”操作生效,剪贴板内容必须是文件
。在上面演示的情况下,这很容易,否则应该创建一个临时文件。
关于java - 从 JavaFX 应用程序复制图像并使用 Windows 粘贴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20491715/