JavaFX2-Ensemble学习(2)-在BorderPane中添加非上下左右中之外的控件和布局技巧

这个效果主要用于为面板增加边角特效,不影响整体布局

实现原理在于重写layoutChildren()方法,在其中为指定的控件进行绝对布局,再将组件添加到children列表中(不用通过设置上下左右中),不过注意,要setManaged(false)是的BorderPane的默认layout不会计算该控件的大小和位置等。见以下代码:

    Region windowResizeButton = new WindowResizeButton(stage, 1020,700);
    BorderPane root = new BorderPane() {
        @Override protected void layoutChildren() {
            super.layoutChildren();
            windowResizeButton.autosize();
            windowResizeButton.setLayoutX(getWidth() - windowResizeButton.getLayoutBounds().getWidth());
            windowResizeButton.setLayoutY(getHeight() - windowResizeButton.getLayoutBounds().getHeight());
        }
    };
    windowResizeButton.setManaged(false);
    this.root.getChildren().add(windowResizeButton);

当然,同样的效果也可以通过StackPane在上面叠加一个透明面板来实现。

你可能感兴趣的:(javafx2,BorderPane)