Javafx 基本拖动过程中的自定义光标&;滴

Javafx 基本拖动过程中的自定义光标&;滴,javafx,javafx-8,Javafx,Javafx 8,我想在拖放事件期间设置自定义光标。我想将光标改写为,例如,经典的开放式指针 b.setOnDragDetected(e -> { Dragboard db = b.startDragAndDrop(TransferMode.MOVE); ClipboardContent content = new ClipboardContent(); content.putString(""); db.setContent(cont

我想在拖放事件期间设置自定义光标。我想将光标改写为,例如,经典的开放式指针

    b.setOnDragDetected(e -> {
        Dragboard db = b.startDragAndDrop(TransferMode.MOVE);
        ClipboardContent content = new ClipboardContent();
        content.putString("");
        db.setContent(content);
        b.setCursor(Cursor.CLOSED_HAND);
    });

    b.setOnDragOver(e -> {
        if (e.getSource() != b)
            e.acceptTransferModes(TransferMode.MOVE);
        b.setCursor(Cursor.CLOSED_HAND);
    });

不幸的是,DrageEvent.acceptTransferModes()正在将光标设置为move/link/copy之一,并忽略我的更改。有没有办法设置其他光标?

因为你没有发布MVCE,我们可以猜出问题出在哪里,我写了一个简单的例子,展示了所有类型的拖放事件。我猜您没有将光标重置为默认光标

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Cursor;
import javafx.scene.ImageCursor;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


    public class MainApp extends Application {

        @Override
        public void start(Stage primaryStage) {

            Pane pane = new Pane();
            pane.setMinSize(400, 400);
            pane.setPadding(new Insets(10));

            Button b = new Button("Drag Source (b)");
            Button c = new Button("Drag Target (c)");
            //here we load a custom image
            Image img = new Image("http://2.bp.blogspot.com/-ipjep9g59YY/VbqLU1vD9qI/AAAAAAAAAOc/CjB4YvRYz_M/s1600/skype-drunk.jpg");

            b.setOnDragOver(event->{
                    if (event.getGestureSource() != b && event.getDragboard().hasString()) {
                        event.acceptTransferModes(TransferMode.MOVE);
                        System.out.println("DragOver (b)");
                    }
                    event.consume();
            });

            b.setOnDragDropped(e -> {
                System.out.println("DragDropped (b)");
                //added following line as cursor wasn´t reseted to default
                b.setCursor(Cursor.DEFAULT);
                e.setDropCompleted(true);
                e.consume();
            });

            b.setOnDragExited(event->{
                event.consume();
                System.out.println("DragExited (b)");
            });

            b.setOnDragDetected(e-> {
                Dragboard db = b.startDragAndDrop(TransferMode.MOVE);

                ClipboardContent content = new ClipboardContent();
                content.putString("Hey i´m b");
                db.setContent(content);
                //b.setCursor(Cursor.OPEN_HAND);
                b.setCursor(new ImageCursor(img));
                e.consume();
            });

            b.setOnDragDone(event->{
            /* the drag and drop gesture ended */
            /* if the data was successfully moved, do something and reset Cursor */
                    if (event.getTransferMode() == TransferMode.MOVE) {                        
                        //b.setCursor(Cursor.DEFAULT);
                    }
                    b.setCursor(Cursor.DEFAULT);
                    event.consume();
            });

            c.setOnDragOver(event->{
                if (event.getGestureSource() != c && event.getDragboard().hasString()) {
                    event.acceptTransferModes(TransferMode.MOVE);
                    System.out.println("DragOver (c)");
                }

                event.consume();

            });

            c.setOnDragDropped(e -> {
                System.out.println("DragDropped (c)");
                e.setDropCompleted(true);
                e.consume();
            });

            c.setOnDragExited(event->{
                event.consume();
                System.out.println("DragExited (c)");
            });


            c.setOnDragDetected(e-> {
                Dragboard db = c.startDragAndDrop(TransferMode.MOVE);

                ClipboardContent content = new ClipboardContent();
                content.putString("Hey i´m c");
                db.setContent(content);
                c.setCursor(Cursor.OPEN_HAND);
                e.consume();
            });
            c.setOnDragDone(event->{
            /* the drag and drop gesture ended */
            /* if the data was successfully moved, do something and reset Cursor */
                if (event.getTransferMode() == TransferMode.MOVE) {
                    c.setCursor(Cursor.DEFAULT);
                }
                event.consume();
            });

            VBox box = new VBox(b,c);
            box.setSpacing(20);

            BorderPane root = new BorderPane(pane, box, null, null, null);
            Scene scene = new Scene(root);
            primaryStage.setScene(scene);
            primaryStage.show();
        }


        public static void main(String[] args) {
            launch(args);
        }
    }

也许你可以设置一个可运行的MVCE?@Rander000你能更详细地解释一下你的意思吗?请参阅帮助中心:@Rander000真丢脸。谢谢你。不必感到羞耻。:)这正是不起作用的。当您在onDragDetected中将光标设置为打开手时,它将在拖到或的过程中自动更改。然而,我的意图是设置一些自定义游标,而不是这两个游标。但是,我之前的回答是您要查找的
Image Image=new Image(“myCustomCursorImage.png”);b、 设置光标(新图像光标(图像))或者我只是不明白问题出在哪里。正如RAndrs00提到的,MVCE总是一个好方法……您的MVCE是完美的。也许我不够精确-我想设置任何与标准光标不同的光标。自定义源于图像或游标类中定义的任何对象。也许gif会显示出来。在您的代码中,在拖动检测期间,有一个OPEN_HAND集合。不幸的是,它被我第一次评论中的游标覆盖了。我想更改它们,但无法完成此操作。我更改了代码以反映自定义光标,希望对您有所帮助