Create resizable borders for empty stage

2019-08-06 02:45发布

I have a blank javafx.stage.Stage with no icons minimize, maximize, and close stage.initStyle(StageStyle.UNDECORATED);. I want to create them myself, because on the line with them will make more Buttons. Because I use an empty style for the Stage, I need to set a resizable borders for my window. There are standard ways to do this? Because to create them as a BorderPane and customize the dragging of the mouse is cumbersome.

3条回答
冷血范
2楼-- · 2019-08-06 02:49

I make resizable borders with mouse cursor position listener, it's work, I have not much time for optimized code:

    public class Resize {
        double eY = 0;
        double eX = 0;
        double eStrtY = 0;
        double eStrtX = 0;
        double sceneStrtY = 0;
        double sceneStrtX = 0;
        double minW = 600;
        double minH = 600;
        double w = 0;
        public Resize(Stage stage, double w) {

            Scene scene = stage.getScene();
            AnchorPane root = (AnchorPane)scene.getRoot();
    this.w = w;
    root.setOnMousePressed(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent e) {
            eStrtY = e.getSceneY();
            eStrtX = e.getSceneX();
            sceneStrtY = e.getSceneY();
            sceneStrtX = e.getSceneX();
        }
    });

    root.setOnMouseMoved(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
        //  double w = 8;
            double X = event.getSceneX();
            double Y = event.getSceneY();
            double sW = scene.getWidth();
            double sH = scene.getHeight();

            if(((X<w)&&(Y<w))||((X<2*w)&&(Y<w))||((Y<2*w)&&(X<w))) {
                scene.setCursor(Cursor.NW_RESIZE);
                eY = stage.getY();
                eX = stage.getX();
                root.setOnMouseDragged(new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent e) {
                        if(((eStrtX<w)&&(eStrtY<w))||((eStrtX<2*w)&&(eStrtY<w))||((eStrtY<2*w)&&(eStrtX<w))) {
                            if(stage.getHeight()+(eY-e.getScreenY())>minH) {
                                stage.setY(e.getScreenY());
                                stage.setHeight(stage.getHeight()+(eY-e.getScreenY()));
                                eY = e.getScreenY();
                            }
                            if(stage.getWidth()+(eX-e.getScreenX())>minW) {
                                stage.setX(e.getScreenX());
                                stage.setWidth(stage.getWidth()+(eX-e.getScreenX()));
                                eX = e.getScreenX();
                            }
                        }
                    }
                });
            }
            else
                if(((X>sW-w)&&(Y<w))||((X>sW-2*w)&&(Y<w))||((X>sW-w)&&(Y<2*w))) {
                    scene.setCursor(Cursor.NE_RESIZE);
                    eY = stage.getY();
                    eX = stage.getX() + stage.getWidth();
                    root.setOnMouseDragged(new EventHandler<MouseEvent>() {
                        @Override
                        public void handle(MouseEvent e) {
                            if(((eStrtX>sW-w)&&(eStrtY<w))||((eStrtX>sW-2*w)&&(eStrtY<w))||((eStrtX>sW-w)&&(eStrtY<2*w))) {
                                if(stage.getHeight()+(eY-e.getScreenY())>minH) {
                                    stage.setY(e.getScreenY());
                                    stage.setHeight(stage.getHeight()+(eY-e.getScreenY()));
                                    eY = e.getScreenY();
                                }
                                if(stage.getWidth()-(eX-e.getScreenX())>minW) {
                                    stage.setWidth(stage.getWidth()-(eX-e.getScreenX()));
                                    eX = e.getScreenX();
                                }
                            }
                        }
                    });
                }
                else
                    if(((X>sW-w)&&(Y>sH-w))||((X>sW-2*w)&&(Y>sH-w))||((X>sW-w)&&(Y>sH-2*w))) {
                        scene.setCursor(Cursor.SE_RESIZE);
                        eY = stage.getY() + stage.getHeight();
                        eX = stage.getX() + stage.getWidth();
                        root.setOnMouseDragged(new EventHandler<MouseEvent>() {
                            @Override
                            public void handle(MouseEvent e) {
                                if(((eStrtX>sW-w)&&(eStrtY>sH-w))||((eStrtX>sW-2*w)&&(eStrtY>sH-w))||((eStrtX>sW-w)&&(eStrtY>sH-2*w))) {
                                    if(stage.getHeight()-(eY-e.getScreenY())>minH) {
                                        stage.setHeight(stage.getHeight()-(eY-e.getScreenY()));
                                        eY = e.getScreenY();
                                    }
                                    if(stage.getWidth()-(eX-e.getScreenX())>minW) {
                                        stage.setWidth(stage.getWidth()-(eX-e.getScreenX()));
                                        eX = e.getScreenX();
                                    }
                                }
                            }
                        });
                    }
                    else
                        if(((X<w)&&(Y>sH-w))||((X<2*w)&&(Y>sH-w))||((X<w)&&(Y>sH-2*w))) {
                            scene.setCursor(Cursor.SW_RESIZE);
                            eY = stage.getY() + stage.getHeight();
                            eX = stage.getX();
                            root.setOnMouseDragged(new EventHandler<MouseEvent>() {
                                @Override
                                public void handle(MouseEvent e) {
                                    if(((eStrtX<w)&&(eStrtY>sH-w))||((eStrtX<2*w)&&(eStrtY>sH-w))||((eStrtX<w)&&(eStrtY>sH-2*w))) {
                                        if(stage.getHeight()-(eY-e.getScreenY())>minH) {
                                            stage.setHeight(stage.getHeight()-(eY-e.getScreenY()));
                                            eY = e.getScreenY();
                                        }
                                        if(stage.getWidth()+(eX-e.getScreenX())>minW) {
                                            stage.setX(e.getScreenX());
                                            stage.setWidth(stage.getWidth()+(eX-e.getScreenX()));
                                            eX = e.getScreenX();
                                        }
                                    }
                                }
                            });
                        }
                        else
                            if(Y<w) {
                                scene.setCursor(Cursor.V_RESIZE);
                                eY = stage.getY();
                                eX = stage.getX() + stage.getWidth();
                                root.setOnMouseDragged(new EventHandler<MouseEvent>() {
                                    @Override
                                    public void handle(MouseEvent e) {
                                        if((eStrtY<w)&&(stage.getHeight()+(eY-e.getScreenY())>minH)) {
                                            stage.setY(e.getScreenY());
                                            stage.setHeight(stage.getHeight()+(eY-e.getScreenY()));
                                            eY = e.getScreenY();
                                        }
                                    }
                                });
                            }
                            else
                                if(Y>sH-w) {
                                    scene.setCursor(Cursor.V_RESIZE);
                                    eY = stage.getY() + stage.getHeight();
                                    root.setOnMouseDragged(new EventHandler<MouseEvent>() {
                                        @Override
                                        public void handle(MouseEvent e) {
                                            if((eStrtY>sH-w)&&(stage.getHeight()-(eY-e.getScreenY())>minH)) {
                                                stage.setHeight(stage.getHeight()-(eY-e.getScreenY()));
                                                eY = e.getScreenY();
                                            }
                                        }
                                    });
                                }
                                else
                                    if(X<w) {
                                        scene.setCursor(Cursor.H_RESIZE);
                                        eX = stage.getX();
                                        root.setOnMouseDragged(new EventHandler<MouseEvent>() {
                                            @Override
                                            public void handle(MouseEvent e) {
                                                if((eStrtX<w)&&(stage.getWidth()+(eX-e.getScreenX())>minW)) {
                                                        stage.setX(e.getScreenX());
                                                        stage.setWidth(stage.getWidth()+(eX-e.getScreenX()));
                                                        eX = e.getScreenX();
                                                }
                                            }
                                        });
                                    }
                                    else
                                        if(X>sW-w) {
                                            scene.setCursor(Cursor.H_RESIZE);
                                            eX = stage.getX() + stage.getWidth();
                                            root.setOnMouseDragged(new EventHandler<MouseEvent>() {
                                                @Override
                                                public void handle(MouseEvent e) {
                                                    if((eStrtX>sW-w)&&(stage.getWidth()-(eX-e.getScreenX())>minW)) {
                                                        stage.setWidth(stage.getWidth()-(eX-e.getScreenX()));
                                                        eX = e.getScreenX();
                                                    }
                                                }
                                            });
                                        }
                                        else
                                            if((X>w)&&(X<sW-w)&&(Y>w)&&(Y<25)) {
                                                root.setOnMouseDragged(new EventHandler<MouseEvent>() {
                                                    @Override
                                                    public void handle(MouseEvent e) {
                                                        if((eStrtX>w)&&(eStrtX<sW-w-25*3)&&(eStrtY>w)&&(eStrtY<w+25)) {
                                                            stage.setX(e.getScreenX()-sceneStrtX);
                                                            stage.setY(e.getScreenY()-sceneStrtY);
                                                        }
                                                    }
                                                });
                                            }
                                            else
                                                scene.setCursor(Cursor.DEFAULT);
        }   
    });

    root.setOnMouseReleased(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent e) {
            eY = e.getScreenY();
            eX = e.getScreenX();
        }
    });
}
查看更多
不美不萌又怎样
3楼-- · 2019-08-06 02:56

Is this link a possible solution for what you require? How to bind stage resizing with resizing of components?

it is also possible that this link could work for you as well:

JavaFX: autoresize stage after adding child to root parent

The main line of code from the second link is

stage.sizeToScene();
查看更多
虎瘦雄心在
4楼-- · 2019-08-06 03:00

altered from a project of mine

This is how it looks

enter image description here

when maximised

enter image description here

package Project

Main.class

public class Main extends Application {
double dragX,dragY;
@Override
public void start(Stage primaryStage) {
    try {
        primaryStage.initStyle(StageStyle.TRANSPARENT);
        HBox hbox = new HBox(10.0);
        hbox.setPrefHeight(70);
        hbox.setFillHeight(true);
        BackgroundFill bf = new BackgroundFill(Color.BLUEVIOLET, new CornerRadii(1), null);
        hbox.setBackground(new Background(bf));

        AnchorPane ap = new AnchorPane();

        ResponsiveButton min = new ResponsiveButton("Minimise");
        min.noDefaultFill = true;
        min.setBorder(Border.EMPTY);
        min.setBackground(new Background(new BackgroundFill(Color.BLUEVIOLET, new CornerRadii(1), null)));
        //min.setPrefSize(32.0,32.0);
        min.setAlignment(Pos.CENTER);
        min.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent arg0) {
                primaryStage.setIconified(true);
            }
        });

        ResponsiveButton max = new ResponsiveButton("Maximise");
        max.setBorder(Border.EMPTY);
        max.setBackground(new Background(new BackgroundFill(Color.BLUEVIOLET, new CornerRadii(1), null)));
        //max.setPrefSize(32.0,32.0);
        max.noDefaultFill = true;
        max.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent arg0) {

                if(primaryStage.isMaximized()){
                    primaryStage.setMaximized(false);
                }else{
                    primaryStage.setMaximized(true);            
                }
            }
        });

        ResponsiveButton logout = new ResponsiveButton("Close me");
        logout.noDefaultFill = true;
        logout.setBorder(Border.EMPTY);
        logout.setBackground(new Background(new BackgroundFill(Color.BLUEVIOLET, new CornerRadii(1), null)));
        //logout.setPrefSize(32.0,32.0);
        logout.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent arg0) {
                primaryStage.close();
            }
        });

        Label label = new Label();
        label.setText("Custom Stage");
        label.setTextFill(Color.WHITESMOKE);
        //label.setAlignment(Pos.CENTER);
        label.setPadding(new Insets(0.0,0.0,0.0,18.0));
        label.setFont(Font.font("Tahoma", FontWeight.BOLD, 25));
        HBox.setHgrow(label, Priority.ALWAYS);  
        hbox.setAlignment(Pos.CENTER);

        hbox.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override public void handle(MouseEvent mouseEvent) {
                if(!primaryStage.isMaximized()){
                    dragX = primaryStage.getX() - mouseEvent.getScreenX();
                    dragY = primaryStage.getY() - mouseEvent.getScreenY();
                    hbox.setCursor(Cursor.MOVE);
                }
            }
        });
        hbox.setOnMouseReleased(new EventHandler<MouseEvent>() {
            @Override public void handle(MouseEvent mouseEvent) {
                if(!primaryStage.isMaximized()){
                    hbox.setCursor(Cursor.HAND);
                    BackgroundFill bf = new BackgroundFill(Color.BLUEVIOLET, new CornerRadii(1), null);
                    hbox.setBackground(new Background(bf));
                }
            }
        });
        hbox.setOnMouseDragged(new EventHandler<MouseEvent>() {
            @Override public void handle(MouseEvent mouseEvent) {
                if(!primaryStage.isMaximized()){
                    primaryStage.setX(mouseEvent.getScreenX() + dragX);
                    primaryStage.setY(mouseEvent.getScreenY() + dragY);
                    BackgroundFill bf = new BackgroundFill(Color.BLANCHEDALMOND, new CornerRadii(1), null);
                    hbox.setBackground(new Background(bf));
                }
            }
        });
        hbox.setOnMouseEntered(new EventHandler<MouseEvent>() {
            @Override public void handle(MouseEvent mouseEvent) {
                if(!primaryStage.isMaximized()){
                    if (!mouseEvent.isPrimaryButtonDown()) {
                        hbox.setCursor(Cursor.HAND);
                    }
                }
            }
        });
        hbox.setOnMouseExited(new EventHandler<MouseEvent>() {
            @Override public void handle(MouseEvent mouseEvent) {
                if (!mouseEvent.isPrimaryButtonDown()) {
                    hbox.setCursor(Cursor.DEFAULT);
                }
            }
        });

        HBox hbox2 = new HBox(10.0);
        HBox.setHgrow(hbox2, Priority.ALWAYS);
        hbox2.getChildren().addAll(min,max,logout);
        hbox.getChildren().addAll(label,hbox2);
        hbox2.setAlignment(Pos.CENTER_RIGHT);
        AnchorPane.setTopAnchor(hbox, 0.0);AnchorPane.setLeftAnchor(hbox, 5.0);AnchorPane.setRightAnchor(hbox, 0.0);
        AnchorPane.setBottomAnchor(ap , 0.0);AnchorPane.setTopAnchor(ap , 40.0);
        AnchorPane.setLeftAnchor(ap , 5.0); AnchorPane.setRightAnchor(ap , 0.0);
        ap .setPrefSize(500, 560);

        AnchorPane main = new AnchorPane();         
        main.getChildren().addAll(hbox,ap);
        Scene sc = new Scene(main,500,500);
        sc.setRoot(main);

        Pane p = new   Pane();
        AnchorPane.setTopAnchor(p, 80.0);
        AnchorPane.setLeftAnchor(p, 60.0);
        Group root = new Group();   
        p.setPrefSize(400, 400);
        p.setBackground(new Background(new BackgroundFill(Color.GOLD,
                null, null)));
        root.getChildren().add(p);
        ap.getChildren().add(p);
        primaryStage.setScene(sc);
        primaryStage.setTitle("Custom window");
        primaryStage.show();
        BubbledLabel bl1 = new BubbledLabel(BubbleSpec.FACE_LEFT_CENTER);
        bl1.relocate(10, 50);
        bl1.setText("I love this new Custom stage");
        bl1.setBackground(new Background(new BackgroundFill(Color.VIOLET,
                null, null)));


        BubbledLabel bl2 = new BubbledLabel(BubbleSpec.FACE_RIGHT_CENTER);
        bl2.relocate(310, 100);
        bl2.setText("We all love it");
        bl2.setBackground(new Background(new BackgroundFill(Color.LIGHTPINK,
                null, null)));

        BubbledLabel bl3 = new BubbledLabel(BubbleSpec.FACE_LEFT_CENTER);
        bl3.relocate(10, 150);
        bl3.setText("Nikitoslav is going love it");
        bl3.setBackground(new Background(new BackgroundFill(Color.YELLOWGREEN,
                null, null)));


        BubbledLabel bl4 = new BubbledLabel(BubbleSpec.FACE_RIGHT_CENTER);
        bl4.relocate(165, 200);
        bl4.setText("hmmm.. you have you asked him???");
        bl4.setBackground(new Background(new BackgroundFill(Color.GREENYELLOW,
                null, null)));

        BubbledLabel bl5 = new BubbledLabel(BubbleSpec.FACE_LEFT_CENTER);
        bl5.relocate(10, 250);
        bl5.setText("yea! yea! ");
        bl5.setBackground(new Background(new BackgroundFill(Color.YELLOWGREEN,
                null, null)));


        BubbledLabel bl6 = new BubbledLabel(BubbleSpec.FACE_RIGHT_CENTER);
        bl6.relocate(250, 300);
        bl6.setText("Oh really!!? oh stop it!!");
        bl6.setBackground(new Background(new BackgroundFill(Color.GREENYELLOW,
                null, null)));


        p.getChildren().addAll(bl1,bl2,bl3,bl4,bl5,bl6);

    } catch(Exception e) {
        e.printStackTrace();
    }
}

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

}

If you just copy the codes here, change ResponsiveButton to Button and remove the Anchorpaneap` and its children, its classes are not added, but if you download the package project there is no problem.

查看更多
登录 后发表回答