JavaFX BorderPane Bottom由Center剪裁

时间:2018-03-25 13:34:06

标签: javafx layout

我有一个BorderPane,中间和底部都有东西。我的问题是,当我垂直收缩窗口时,中心部分会夹在底部。

以下是一个例子:https://o7planning.org/en/10629/cache/images/i/2768251.gif

当窗口垂直调整大小时,底部按钮"消失"。我希望底部始终以相同的尺寸显示(即,由其内容给出的最小尺寸),而中心可以根据需要缩小。

因此,窗口应具有最小高度,即底部的高度(永远不应隐藏)。

以下是示例中的代码:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class BorderPaneDemo extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        BorderPane root = new BorderPane();

        root.setPadding(new Insets(15, 20, 10, 10));

        // TOP
        Button btnTop = new Button("Top");
        btnTop.setPadding(new Insets(10, 10, 10, 10));
        root.setTop(btnTop);
        // Set margin for top area.
        BorderPane.setMargin(btnTop, new Insets(10, 10, 10, 10));


        // LEFT
        Button btnLeft = new Button("Left");
        btnLeft.setPadding(new Insets(5, 5, 5, 5));
        root.setLeft(btnLeft);
        // Set margin for left area.
        BorderPane.setMargin(btnLeft, new Insets(10, 10, 10, 10));

        // CENTER
        Button btnCenter = new Button("Center");
        btnCenter.setPadding(new Insets(5, 5, 5, 5));
        root.setCenter(btnCenter);
         // Alignment.
         BorderPane.setAlignment(btnCenter, Pos.BOTTOM_CENTER);

        // RIGHT
        Button btnRight = new Button("Right");
        btnRight.setPadding(new Insets(5, 5, 5, 5));
        root.setRight(btnRight);
        // Set margin for right area.
        BorderPane.setMargin(btnRight, new Insets(10, 10, 10, 10));

        // BOTTOM
        Button btnBottom = new Button("Bottom");
        btnBottom.setPadding(new Insets(5, 5, 5, 5));
        root.setBottom(btnBottom);
        // Alignment.
        BorderPane.setAlignment(btnBottom, Pos.TOP_RIGHT);

        // Set margin for bottom area.
        BorderPane.setMargin(btnBottom, new Insets(10, 10, 10, 10));

        Scene scene = new Scene(root, 550, 250);

        primaryStage.setTitle("BorderPane Layout Demo");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

}

1 个答案:

答案 0 :(得分:0)

我无法使用BorderPane实现此目的。我认为将BorderPane's顶部和中心Nodes最小高度设置为零将实现此目的。我能够通过HBox作为根,VBox保持中间Nodes来实现此目标。

然后我在Top的根Node和中心的根Node上设置最小高度。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class BorderPaneDemo extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        Button btnTop = new Button("Top");
        Button btnLeft = new Button("Left");
        Button btnCenter = new Button("Center");
        Button btnRight = new Button("Right");
        Button btnBottom = new Button("Botton");


        VBox vbCenter = new VBox(btnTop, btnCenter, btnBottom);
        HBox root = new HBox(btnLeft, vbCenter, btnRight);

        HBox.setHgrow(vbCenter, Priority.ALWAYS);
        VBox.setVgrow(btnCenter, Priority.ALWAYS);

        btnTop.setMaxWidth(Double.MAX_VALUE);
        btnCenter.setMaxWidth(Double.MAX_VALUE);
        btnBottom.setMaxWidth(Double.MAX_VALUE);

        btnLeft.setMaxHeight(Double.MAX_VALUE);
        btnCenter.setMaxHeight(Double.MAX_VALUE);
        btnRight.setMaxHeight(Double.MAX_VALUE);

        //The Top and Center root node needs to have a min height of zero
        btnTop.setMinHeight(0);
        btnCenter.setMinHeight(0);

        Scene scene = new Scene(root, 550, 250);

        primaryStage.setTitle("BorderPane Layout Demo");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

}

enter image description here