有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从JavaFX应用程序复制图像并使用Windows粘贴

我正在开发一个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);
        }
    });

例如,如下图所示

Copy Image From aaplication

并希望在使用Windows功能菜单的位置粘贴

Paste Image at spectific location


共 (1) 个答案

  1. # 1 楼答案

    使用^{}^{},例如:

    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上下文菜单的“粘贴”操作正常工作,剪贴板内容必须是File。在上面演示的例子中,这很容易,否则应该创建一个临时文件