如何拖动JavaFX的未修饰窗口(阶段)

javafx-2 javafx javafx-8

13090 观看

2回复

93 作者的声誉

我有这个未修饰的窗口:

public static void initStartPage(final Stage primaryStage) {

        final Stage startPage = new Stage();
        startPage.initStyle(StageStyle.UNDECORATED);
        //startPage.initOwner(primaryStage);
        //startPage.toFront();
        Scene scene = new Scene(agentsPanel(), 900, 500);
        startPage.setScene(scene);
        startPage.show();

    }

我想知道如何让它成为一个可拖动的未装饰的窗户?我想在用户用鼠标右键选择窗口时更改其位置,然后在按住鼠标按钮的同时移动鼠标。

PS我测试了这个解决方案,但它无法正常工作:

private static FlowPane flow;
    private static BorderPane bpi;

    public static void initStartPage(final Stage primaryStage) {

        final Stage startPage = new Stage();
        startPage.initStyle(StageStyle.UNDECORATED);
        startPage.initOwner(primaryStage);
        //startPage.toFront();
        Scene scene = new Scene(agentsPanel(primaryStage), 900, 500);
        startPage.setScene(scene);
        startPage.show();

    }

    private static double xOffset = 0;
    private static double yOffset = 0;

    public static BorderPane agentsPanel(final Stage primaryStage) {

        BorderPane bp = new BorderPane();
        bp.setPrefSize(900, 500);
        bp.setMaxSize(900, 500);

        HBox thb = new HBox(10); // Set spacing between each child into the HBox
        thb.setPadding(new Insets(15, 15, 15, 15));


        HBox bhb = new HBox(10); // Set spacing between each child into the HBox
        bhb.setPadding(new Insets(15, 15, 15, 15));

        bp.setTop(thb);
        bp.setBottom(bhb);
        bp.setCenter(navigationPanel());


        bp.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                xOffset = event.getSceneX();
                yOffset = event.getSceneY();
            }
        });
        bp.setOnMouseDragged(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                primaryStage.setX(event.getScreenX() - xOffset);
                primaryStage.setY(event.getScreenY() - yOffset);
            }
        });

        return bp;

    }
作者: Peter Penzov 的来源 发布者: 2013 年 8 月 11 日

回应 (2)


25

865 作者的声誉

决定

只需将setOnMousePressed方法更改为:

bp.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                xOffset = primaryStage.getX() - event.getScreenX();
                yOffset = primaryStage.getY() - event.getScreenY();
            }
        });

和你的setOnMouseDragged到这个:

bp.setOnMouseDragged(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                primaryStage.setX(event.getScreenX() + xOffset);
                primaryStage.setY(event.getScreenY() + yOffset);
            }
        });
作者: Eeliya 发布者: 11.08.2013 11:37

0

14 作者的声誉

你可以使用它,适用于舞台和警报(基本上是另一个阶段,但对于小狗消息)。节点是该阶段的主节点

//Make a Stage Draggable
public void DraggableStage(Node node, Stage stage, Alert alert ) {
    double[] xOffset = {0}, yOffset = {0};
    node.setOnMousePressed(event -> {
        if (stage != null && alert == null){
            xOffset[0] = stage.getX() - event.getScreenX();
            yOffset[0] = stage.getY() - event.getScreenY();
        } else if(stage == null && alert != null){
            xOffset[0] = alert.getX() - event.getScreenX();
            yOffset[0] = alert.getY() - event.getScreenY();
        }
    });

    node.setOnMouseDragged(event -> {
        if (stage != null && alert == null){
            stage.setX(event.getScreenX() + xOffset[0]);
            stage.setY(event.getScreenY() + yOffset[0]);
        } else if(stage == null && alert != null){
            alert.setX(event.getScreenX() + xOffset[0]);
            alert.setY(event.getScreenY() + yOffset[0]);
        }
    });
}
作者: Dan 发布者: 19.08.2019 07:46
32x32