为空舞台创建可调整大小的边框

时间:2015-07-29 10:57:46

标签: java css javafx border stage

我有一个空白Bar没有图标最小化最大化关闭 javafx.stage.Stage。我想自己创建它们,因为与它们一致会产生更多stage.initStyle(StageStyle.UNDECORATED);。因为我为Buttons使用了一个空样式,所以我需要为我的窗口设置一个可调整大小的边框。有标准方法可以做到这一点?因为将它们创建为Stage并自定义拖动鼠标是很麻烦的。

3 个答案:

答案 0 :(得分:2)

从我的项目中改变

这就是它的外观

enter image description here

最大化时

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

}

如果你只是在这里复制代码,将ResponsiveButton更改为按钮并删除Anchorpane ap`及其子代,则不添加其类,但如果下载软件包项目则没有问题

答案 1 :(得分:0)

此链接是否是您需要的可能解决方案? How to bind stage resizing with resizing of components?

此链接也可能适用于您:

JavaFX: autoresize stage after adding child to root parent

第二个链接的主要代码是

stage.sizeToScene();

答案 2 :(得分:0)

我使用鼠标光标位置监听器制作可调整大小的边框,它的工作,我没有太多时间来优化代码:

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