【问题标题】:Copy image from JavaFX application and paste it using Windows从 JavaFX 应用程序复制图像并使用 Windows 粘贴
【发布时间】:2013-12-27 19:22:34
【问题描述】:

我正在开发一个 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 功能菜单粘贴到位置。

【问题讨论】:

    标签: java javafx javafx-2 fxml javafx-8


    【解决方案1】:

    使用ClipboardClipboardContent,例如如:

    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。在上面演示的情况下,这很容易,否则应该创建一个临时文件。

    【讨论】:

    • 我试过了,但它不起作用。我在上面的问题上进行了编辑。检查。粘贴选项不来
    • 对于这种情况,您只需将文件放入剪贴板,请参阅编辑。
    • 这适用于 Gimp 和 Paint,但不适用于 Microsoft Word。您有什么想法吗?但它适用于 Microsoft Powerpoint..
    • 也许可以试试ClipboardContent.putImage() 方法?
    猜你喜欢
    • 2015-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 2010-10-04
    相关资源
    最近更新 更多