How to resize Javafx undecorated stage?

非 Y 不嫁゛ 提交于 2019-12-11 03:42:21

问题


I am new to JavaFX technology, currently I am working on javafx application, in which there is undecorated stage,I am able to move it on screen using below code , but I am not able to resize this window from bottom right corner, can anyone suggest me the solution.

public void loadPanel(final Stage stage) {

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


        this.stage = stage;
        this.stage.initStyle(StageStyle.UNDECORATED);
        this.stage.setScene(scene);
        this.stage.show();
    }


回答1:


If you want to resize your stage only on bottom right corner, you need at first an if statement to check, whether your mouse is there. In addition to this you need to distinguish in your dragFunction whether this is the case or not. If this is the case, you need the value your mouse moved and add this to the actuall size of your stage. The value -10 in the code (setOnMousePressed) is like a border so that you have a bigger area where you can click to resize the window. You can adjust this value like you want. This code snippets may be helpful:

    private Boolean resizebottom = false;
    private double dx;
    private double dy;

    myPane.setOnMousePressed(new EventHandler<MouseEvent>() {
        public void handle(MouseEvent event) {
            if (event.getX() > stage.getWidth() - 10
                    && event.getX() < stage.getWidth() + 10
                    && event.getY() > stage.getHeight() - 10
                    && event.getY() < stage.getHeight() + 10) {
                resizebottom = true;
                dx = stage.getWidth() - event.getX();
                dy = stage.getHeight() - event.getY();
            } else {
                resizebottom = false;
                xOffset = event.getSceneX();
                yOffset = event.getSceneY();
            }
        }
    });

    myPane.setOnMouseDragged(new EventHandler<MouseEvent>() {
        public void handle(MouseEvent event) {
            if (resizebottom == false) {
                stage.setX(event.getScreenX() - xOffset);
                stage.setY(event.getScreenY() - yOffset);
            } else {
                stage.setWidth(event.getX() + dx);
                stage.setHeight(event.getY() + dy);
            }
        }
    });



回答2:


you can make your stage public static and use this code and customize as per your code:

stage1.setWidth(Your double value);
stage1.setHeight(Your double value); 


来源:https://stackoverflow.com/questions/24106406/how-to-resize-javafx-undecorated-stage

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!