window 最大化时如何自动调整 JavaFX 容器的大小?

How to automatically resize JavaFX containers when window is maximized?

基本上,我有以下文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXTabPane?>
<?import com.jfoenix.controls.JFXTreeView?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

   <BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="566.0" prefWidth="753.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gui.controller">
    <center>
        <JFXTabPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" BorderPane.alignment="CENTER">
            <tabs>
                <Tab text="TAB">
                    <content>
                        <AnchorPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                            <children>
                                <VBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="495.0" prefWidth="753.0">
                                    <children>
                                        <StackPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="150.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
                                            <children>
                                                <HBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" nodeOrientation="LEFT_TO_RIGHT" prefHeight="100.0" prefWidth="200.0">
                                                    <children>
                                                        <JFXTreeView fx:id="treeView" />
                                                        <TableView  maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="277.0" prefWidth="753.0">
                                                            <columns>
                                                                <TableColumn maxWidth="-1.0" text="COL" />
                                                                <TableColumn text="COL" />
                                                                <TableColumn text="COL" />
                                                                <TableColumn text="COL" />
                                                                <TableColumn minWidth="0.0" text="COL" />
                                                                <TableColumn text="COL" />
                                                            </columns>
                                                            <columnResizePolicy>
                                                                <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                                                            </columnResizePolicy>
                                                        </TableView>
                                                    </children>
                                                </HBox>
                                            </children>
                                        </StackPane>
                                    </children>
                                </VBox>
                            </children>
                        </AnchorPane>
                    </content>
                </Tab>
            </tabs>
        </JFXTabPane>
    </center>
</BorderPane>

主要class加载FXML文档:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Main extends Application {

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

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

        BorderPane parent = (BorderPane)  FXMLLoader.load(getClass().getResource("document.fxml"));
        Scene scene = new Scene(parent);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

当我最大化 window 时,table 和其余容器不会相应地调整大小。我试图在 SceneBuilder 中将所有容器的 Max WidthMax Height 设置为 MAX_VALUE,但实际上没有任何反应。

有没有一种方法可以自动扩展容器,以便在 window 最大化时它们可以使用所有 space?

这是一个开始:将随场景一起增长的边框窗格:

public void start(Stage stage) {

    BorderPane root = new BorderPane();
    root.setStyle("-fx-background-color: yellow");
    root.setCenter(new Text("center"));
    root.setTop(new Text("top"));
    root.setBottom(new Text("bottom"));

    Scene scene = new Scene(root);
    root.prefHeightProperty().bind(scene.heightProperty());
    root.prefWidthProperty().bind(scene.widthProperty());
    stage.setScene(scene);
    stage.show();
}

在花了一些时间调查问题之后。已更正问题的步骤是:

1- 将所有锚定窗格约束设置为 0(在 VBox 标记中)将允许子容器占用父容器提供的所有 space。

<VBox AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">

2- 将 TabeView 的 Hgrow 属性设置为 Always 将允许 table 在发现 space.

时水平增长 3- 对于其余的容器,保持默认设置并将最大高度和最大宽度设置为 USE_COMPUTED_SIZE 即可。