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

时间:2018-02-24 07:30:47

标签: javafx javafx-8 fxml scenebuilder

基本上,我有以下文件:

<?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>

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

当我最大化窗口时,表格和其他容器不会相应地调整大小。我尝试将所有容器的Max WidthMax Height设置为SceneBuilder中的MAX_VALUE,但没有真正发生。

有没有办法自动扩展容器,以便在窗口最大化时可以使用所有空间?

2 个答案:

答案 0 :(得分:1)

花了一些时间调查问题。纠正问题的步骤是:

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

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

2-将TabeView的Hgrow属性设置为Always将允许表格在找到空格时水平增长。

3-对于其余的容器,保持默认设置并将Max Height和Max Width设置为USE_COMPUTED_SIZE就可以了。

答案 1 :(得分:0)

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

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