如何拖动未修饰的窗口

时间:2013-08-11 16:09:59

标签: javafx-2 javafx javafx-8

我有这个未修饰的窗口:

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();

    }

我很感兴趣如何让它拖动可拖动?我希望我将鼠标光标放在它上面以拖动它来改变它的位置。

P.S

我测试了这个解决方案,但它不起作用:

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;

    }

3 个答案:

答案 0 :(得分:23)

只需将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);
            }
        });

答案 1 :(得分:3)

@Eeliyaanswer运作良好。但是,如果使用以下代码,则代码会缩短,因为我们不需要声明变量。

setOnMouseDragged触发时,我们需要一次又一次setOnMouseDragged,但我认为这不是问题的根源。

bp.setOnMousePressed(pressEvent -> {
    bp.setOnMouseDragged(dragEvent -> {
        primaryStage.setX(dragEvent.getScreenX() - pressEvent.getSceneX());
        primaryStage.setY(dragEvent.getScreenY() - pressEvent.getSceneY());
    });
});

希望有帮助

答案 2 :(得分:0)

您可以使用它,它既适用于一个阶段,也适用于警报(基本上是另一个阶段,但适用于小狗消息)。该节点是该阶段的主要节点

//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]);
        }
    });
}