如何拖动未修饰的窗口 [英] How to drag undecorated window

查看:83
本文介绍了如何拖动未修饰的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个未修饰的窗口:

I have this undecorated window:

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

    }

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

I'm interested how I can make it drag dragable? I want the I pres the mouse cursor over it to drag it to change it's position.

PS

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

I tested this solution but it's not working:

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;

    }


推荐答案

改变你的setOnMousePressed方法:

Just change your setOnMousePressed method to this:

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

和你的setOnMouseDragged到:

and your setOnMouseDragged to this:

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

这篇关于如何拖动未修饰的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆