拖放侦听器和处理程序无法正常工作[JavaFX] [关闭]

问题描述 投票:1回答:1

所以我试图使用javafx库在java中实现基本的拖放。 setOnDragDetected工作正常但是我拖动元素的窗格没有响应任何拖放事件。

“要拖动的窗格为蓝色的问题的实际图像,要拖动的元素是矩形。”

我查看了不同的教程和文章,他们的源代码也没有帮助。尝试和没有lambdas。

要拖动的窗格上使用的代码

public abstract class VueEtapeIG extends Pane {
       public VueEtapeIG(...){
            //some code..
            this.setOnDragDetected((MouseEvent event) -> {
            //activate();
            Dragboard db = this.startDragAndDrop(TransferMode.MOVE);
            ClipboardContent content = new ClipboardContent();
            // Store node ID in order to know what is dragged.
            content.putString(this.getId());
            db.setContent(content);
            System.out.println("setOnDragDetected");
            event.consume();
        });
    }
}

Pane上使用的代码被拖入:

public class VueDessin extends Pane implements Observer
{
     public VueDessin(...){
        //some code..
        setOnDragOver((DragEvent event) -> {
            if (event.getGestureSource() != this && 
                      event.getDragboard().hasString()) {
                System.out.println("acceptTransferModes");
                event.acceptTransferModes(TransferMode.MOVE);
            }
            System.out.println("setOnDragOver");
            event.consume();
        });

        setOnDragDropped((DragEvent event) -> {
            Dragboard db = event.getDragboard();
            System.out.println("Dropped!");
            // Get item id here, which was stored when the drag started.
            boolean success = false;
            // If this is a meaningful drop...
            if (db.hasString()) {
                String nodeId = db.getString();
                //Search for the etape dropped
            }
            event.setDropCompleted(success);
            event.consume();
        });
    }
}

我希望这些偶数监听器中的print语句能够工作,然后我可以实现更多其他功能,但目前看来听众和处理程序甚至都没有工作

java javafx drag-and-drop javafx-8
1个回答
1
投票

我的代码无法找到任何明显的错误。所以无法指出确切的问题。也许如果你尝试使用Minimal, Complete, and Verifiable example,你可能会知道。请查看我尝试过的以下演示,它运行良好。试着弄清楚你的代码出了什么问题(与此相比)。

import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.image.ImageView;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class DragDemo extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        StackPane root = new StackPane();
        Scene sc = new Scene(root, 600, 600);
        stage.setScene(sc);
        stage.show();

        HBox hb = new HBox();
        VBox imageBox = new VBox();
        Node node1 = buildNode("red");
        Node node2 = buildNode("yellow");
        imageBox.getChildren().addAll(node1,node2);

        StackPane displayBox = new StackPane();
        displayBox.setStyle("-fx-border-width:2px;-fx-border-color:black;");
        HBox.setHgrow(displayBox,Priority.ALWAYS);
        hb.getChildren().addAll(imageBox,displayBox);
        root.getChildren().add(hb);

        displayBox.setOnDragOver(event -> {
            if (event.getGestureSource() != displayBox &&
                    event.getDragboard().hasString()) {
                event.acceptTransferModes(TransferMode.MOVE);
            }
            event.consume();
        });

        displayBox.setOnDragEntered(event -> {
            if (event.getGestureSource() != displayBox && event.getDragboard().hasString()) {
                displayBox.setStyle("-fx-border-width:2px;-fx-border-color:black;-fx-opacity:.4;-fx-background-color:"+event.getDragboard().getString());
            }
            event.consume();
        });

        displayBox.setOnDragExited(event -> {
            if(!event.isAccepted()) {
                displayBox.setStyle("-fx-border-width:2px;-fx-border-color:black;");
                event.consume();
            }
        });

        displayBox.setOnDragDropped(event -> {
            Dragboard db = event.getDragboard();
            boolean success = false;
            if (db.hasString()) {
                displayBox.setStyle("-fx-border-width:2px;-fx-border-color:black;-fx-background-color: "+db.getString());
                success = true;
            }
            event.setDropCompleted(success);
            event.consume();
        });

    }
    private Node buildNode(String color){
        StackPane node = new StackPane();
        node.setPrefSize(200,200);
        node.setStyle("-fx-background-color:"+color);
        node.setOnDragDetected(event -> {
            Dragboard db = node.startDragAndDrop(TransferMode.MOVE);
            ClipboardContent content = new ClipboardContent();
            content.putImage(node.snapshot(new SnapshotParameters(),null));
            content.putString(color);
            db.setContent(content);
            event.consume();
        });
        return node;
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.