Javafx FXML 布局拖放

Javafx FXML layout drag and drop

我喜欢使用 FXML 布局在 Javafx 中编写九人莫里斯游戏,我将使用 Scene Builder 来处理图形部分。

有人可以给我写一个关于拖放的小例子吗?因为是九人莫里斯游戏所以我只喜欢拖,有一些可以丢棋子的点。

拖放的简单示例

public class JavaFX_DragAndDrop extends Application
{

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

    @Override
    public void start(Stage primaryStage)
    {
        primaryStage.setTitle("Drag & Drop Example");
        Group root = new Group();
        Scene scene = new Scene(root, 350, 300);

        HBox hBox = new HBox();

        setupGestureTarget(scene, hBox);

        root.getChildren().add(hBox);
        primaryStage.setScene(scene);
        primaryStage.show();
        primaryStage.show();
    }

    void setupGestureTarget(final Scene target, final HBox targetBox)
    {

        target.setOnDragOver(new EventHandler<DragEvent>()
        {
            @Override
            public void handle(DragEvent event)
            {
                /* data is dragged over the target */
                System.out.println("onDragOver");

                Dragboard db = event.getDragboard();
                if (db.hasFiles())
                {
                    event.acceptTransferModes(TransferMode.COPY);
                }

                event.consume();
            }
        });

        target.setOnDragDropped(new EventHandler<DragEvent>()
        {
            @Override
            public void handle(DragEvent event)
            {
                /* data dropped */
                System.out.println("onDragDropped");

                Dragboard db = event.getDragboard();

                if (db.hasFiles())
                {

                    for (File file : db.getFiles())
                    {
                        String absolutePath = file.getAbsolutePath();
                        Image dbimage = new Image(absolutePath);
                        ImageView dbImageView = new ImageView();
                        dbImageView.setImage(dbimage);
                        targetBox.getChildren().add(dbImageView);
                    }

                    event.setDropCompleted(true);
                }
                else
                {
                    event.setDropCompleted(false);
                }

                event.consume();
            }
        });

    }
}