将节点拖到另一个不工作JavaFX的节点中

时间:2017-11-03 09:37:34

标签: java javafx java-8 drag-and-drop javafx-8

我正在尝试拖动节点并将其放在另一个节点上。 这是我的代码

package hell;

/**
  * Created by kofi on 6/1/17.
*/

import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.*;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.stage.*;

public class Hello extends Application {

private static Stage PRIMARY_STAGE;
private static final Rectangle SEED = new Rectangle(70, 40);
private static Group ROOT;
private static Circle TRIAL; // Used for test purpose only

private Point2D anchorPt;
private Point2D previousLocation;

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

public void start(Stage primaryStage) {
    primaryStage.setTitle("Playing around with Group nodes");
    PRIMARY_STAGE = primaryStage;
    Group root = new Group();
    ROOT = root;
    Scene scene = new Scene(root, 320, 240);

    drawLines();
    drawSeeds();
    //drawLines();

    root.getChildren().add(TRIAL);
    PRIMARY_STAGE.setScene(scene);
    PRIMARY_STAGE.show();
}

private void drawSeeds() {
    ROOT.getChildren().addAll(
            upperLeft(), upperRight(), center(),
            lowerLeft(), lowerRight(), centerBottom(),
            centerLeft(), centerRight(), centerTop()
    );
}

private void setSeedProperty(Circle circle) {
    circle.setStroke(Color.DARKGRAY);
    circle.setFill(Color.rgb(0, 0, 0, 0.1));
}

private Node upperLeft() {
    Circle circle = new Circle(20);
    setSeedProperty(circle);

    circle.centerXProperty().bind(SEED.widthProperty().divide(2));
    circle.centerYProperty().bind(SEED.heightProperty().divide(2));

    Circle trial = new Circle(20);
    trial.centerXProperty().bind(circle.centerXProperty());
    trial.centerYProperty().bind(circle.centerYProperty());
    trial.translateYProperty().bind(circle.translateYProperty());
    trial.translateXProperty().bind(circle.translateXProperty());
    trial.setFill(Color.RED);

    trial.setOnMousePressed(mouseEvent -> anchorPt = new Point2D(mouseEvent.getScreenX(), mouseEvent.getScreenY()));

    trial.setOnDragDetected(event -> {
        Dragboard db = trial.startDragAndDrop(TransferMode.ANY);
        event.consume();
    });
    trial.setOnDragDone(event -> {
        /* the drag-and-drop gesture ended */
        System.out.println("onDragDone");
        /* if the data was successfully moved, clear it */
        if (event.getTransferMode() == TransferMode.MOVE) {
            // TODO: To be done later
        }

        event.consume();
    });

    previousLocation = new Point2D(trial.getCenterX(),
            trial.getCenterY());
    // dragging the entire node (trial)
    trial.setOnMouseDragged(mouseEvent -> {
        if (anchorPt != null && previousLocation != null) {

            trial.centerXProperty().unbind();
            trial.centerYProperty().unbind();
            trial.translateYProperty().unbind();
            trial.translateXProperty().unbind();


            trial.centerXProperty().setValue(previousLocation.getX()
                    + mouseEvent.getScreenX()
                    - anchorPt.getX());
            trial.centerYProperty().setValue(previousLocation.getY()
                    + mouseEvent.getScreenY()
                    - anchorPt.getY());
        }
    });

    // set the current location
    trial.setOnMouseReleased(mouseEvent
            -> previousLocation = new Point2D(trial.getCenterX(),
            trial.getCenterY())
    );

    trial.setOnMouseMoved(mouseEvent -> trial.setCursor(Cursor.OPEN_HAND));
    trial.setOnMouseExited(mouseEvent -> trial.setFill(Color.RED));
    TRIAL = trial;


    return circle;
}

private Node upperRight() {
    Circle circle = new Circle(20);
    setSeedProperty(circle);


    circle.centerXProperty().bind(PRIMARY_STAGE.widthProperty().subtract(SEED.widthProperty().divide(2)));
    circle.translateYProperty().bind(SEED.heightProperty().divide(2));

    circle.setOnDragOver(dragEvent -> {
        // Dragboard db=dragEvent.getDragboard();
        // circle.setFill(Color.BLACK);
        System.out.println("Dragged over");
    });

    circle.setOnMouseDragOver(mouseDragEvent -> {
        System.out.println("draged over");
    });

    return circle;
}

private Node center() {
    Circle circle = new Circle(20);
    setSeedProperty(circle);


    circle.translateYProperty().bind(PRIMARY_STAGE.heightProperty().divide(2).subtract(15));
    circle.translateXProperty().bind(PRIMARY_STAGE.widthProperty().divide(2).subtract(1));

    return circle;
}

private Node centerTop() {
    Circle circle = new Circle(20);

    setSeedProperty(circle);

    circle.centerXProperty().bind(PRIMARY_STAGE.widthProperty().divide(2));
    circle.translateYProperty().bind(SEED.heightProperty().divide(2));
    circle.setOnDragOver(event -> {
        circle.setFill(Color.BLUE);
    });

    circle.setOnDragOver(event -> {
        /* data is dragged over the target */
        System.out.println("onDragOver");

        event.acceptTransferModes(TransferMode.MOVE);

        event.consume();
    });

    circle.setOnDragEntered(event -> {
        /* the drag-and-drop gesture entered the target */
        System.out.println("onDragEntered");
        /* show to the user that it is an actual gesture target */

        circle.setFill(Color.GREEN);


        event.consume();
    });

    circle.setOnDragExited(event -> {
        /* mouse moved away, remove the graphical cues */
        // circle.setFill(Color.BLACK);
        // TODO: To be done later
        // event.consume();
    });

    circle.setOnDragDropped(event -> {
        /* data dropped */
        System.out.println("onDragDropped");
        Dragboard db = event.getDragboard();
        event.setDropCompleted(true);

        event.consume();
    });
    return circle;
}

private Node centerBottom() {
    Circle circle = new Circle(20);

    setSeedProperty(circle);
    circle.centerXProperty().bind(PRIMARY_STAGE.widthProperty().divide(2));
    circle.centerYProperty().bind(PRIMARY_STAGE.heightProperty().subtract(50));


    return circle;
}

private Node centerRight() {
    Circle circle = new Circle(20);

    setSeedProperty(circle);

    circle.centerYProperty().bind(PRIMARY_STAGE.heightProperty().divide(2).subtract(15));
    circle.centerXProperty().bind(PRIMARY_STAGE.widthProperty().subtract(SEED.widthProperty().subtract(35)));

    return circle;
}

private Node centerLeft() {
    Circle circle = new Circle(20);

    setSeedProperty(circle);

    circle.centerYProperty().bind(PRIMARY_STAGE.heightProperty().divide(2).subtract(15));
    circle.translateXProperty().bind(SEED.widthProperty().divide(2));

    return circle;
}

private Node lowerLeft() {
    Circle circle = new Circle(20);

    setSeedProperty(circle);

    circle.centerYProperty().bind(
            PRIMARY_STAGE.heightProperty().subtract(50
            )
    );
    circle.centerXProperty().bind(
            PRIMARY_STAGE.widthProperty().subtract(
                    PRIMARY_STAGE.widthProperty().subtract(
                            SEED.widthProperty())).subtract(
                    SEED.widthProperty().divide(2)
            )
    );

    return circle;
}

private Node lowerRight() {
    Circle circle = new Circle(20);
    setSeedProperty(circle);

    circle.translateYProperty().bind(PRIMARY_STAGE.heightProperty().subtract(50));
    circle.translateXProperty().bind(PRIMARY_STAGE.widthProperty().subtract(35));
    return circle;
}

private void drawLines() {
    ROOT.getChildren().addAll(
            cornerLines(),
            leftDiagonalLine(), rightDiagonalLine(),
            horizontalLine(), verticalLine()
    );
}

private Rectangle cornerLines() {
    Rectangle cornerLine = new Rectangle();

    cornerLine.yProperty().bind(SEED.heightProperty().divide(2));
    cornerLine.xProperty().bind(SEED.widthProperty().divide(2));
    cornerLine.widthProperty().bind(PRIMARY_STAGE.widthProperty().subtract(SEED.widthProperty()));
    cornerLine.heightProperty().bind(PRIMARY_STAGE.heightProperty().subtract(SEED.widthProperty()));

    cornerLine.setFill(Color.rgb(0, 0, 0, 0));
    cornerLine.setStroke(Color.BLACK);
    return cornerLine;
}

private Line leftDiagonalLine() {
    Line line = new Line();

    line.startXProperty().bind(SEED.widthProperty().divide(2));
    line.startYProperty().bind(SEED.heightProperty().divide(2));
    line.endYProperty().bind(PRIMARY_STAGE.heightProperty().subtract(SEED.heightProperty()).subtract(9));
    line.endXProperty().bind(PRIMARY_STAGE.widthProperty().subtract(SEED.widthProperty().divide(2)));

    return line;
}

private Line rightDiagonalLine() {
    Line line = new Line();

    line.startXProperty().bind(PRIMARY_STAGE.widthProperty().subtract(SEED.widthProperty().divide(2)));
    line.translateYProperty().bind(SEED.heightProperty().divide(2));
    line.endYProperty().bind(
            PRIMARY_STAGE.heightProperty().subtract(70
            )
    );
    line.endXProperty().bind(
            PRIMARY_STAGE.widthProperty().subtract(
                    PRIMARY_STAGE.widthProperty().subtract(
                            SEED.widthProperty())).subtract(
                    SEED.widthProperty().divide(2)
            )
    );

    return line;
}

private Line verticalLine() {
    Line line = new Line();
    line.startXProperty().bind(PRIMARY_STAGE.widthProperty().divide(2));
    line.translateYProperty().bind(SEED.heightProperty().divide(2));
    line.endXProperty().bind(PRIMARY_STAGE.widthProperty().divide(2));
    line.endYProperty().bind(
            PRIMARY_STAGE.heightProperty().subtract(70
            )
    );
    return line;
}

private Line horizontalLine() {
    Line line = new Line();
    line.startYProperty().bind(PRIMARY_STAGE.heightProperty().divide(2).subtract(14));
    line.translateXProperty().bind(SEED.widthProperty().divide(2));
    line.endYProperty().bind(PRIMARY_STAGE.heightProperty().divide(2).subtract(14));
    line.endXProperty().bind(PRIMARY_STAGE.widthProperty().subtract(SEED.widthProperty()));
    return line;
}}

因此我尝试将TRIAL拖离upperLeft()并将其放入centerTop()
因此,如果我将桌面上的图标拖到centerTop(),控制台会记录Dragged Over以显示代码正在接收端工作。
现在,当我将TRIALupperLeft()拖到centerTop()时,控制台中没有任何内容记录,这意味着接收端无法识别正在拖动它的元素。
如何将TRIALupperLeft()拖到centerTop()?我有什么不对的吗?

1 个答案:

答案 0 :(得分:0)

你忘了这个Node.startDragAndDrop(TransferMode... transferModes)

修改

trial.setOnDragDetected(event -> {
        Dragboard db = trial.startDragAndDrop(TransferMode.ANY);
        ClipboardContent content = new ClipboardContent();
        content.putString("hhhh");
        db.setContent(content);
        event.consume();
    });

向ClipboardContent添加内容,以在内部识别拖动和放大器。下降。

是拖拉&删除你的用例实际需要的东西?它可能不适用于您的翻译动画