我可以在不绑定每个小部件的情况下使用 window JavaFx 的大小调整场景大小吗?

Can i resize scene with the size of window JavaFx without binding each widget?

在我的应用程序中,我正在切换 fxml 文件以更改视图,因为我没有正确了解 Scene 和 Parent 根的概念。我所有的 fxml 文件都有一个 AnchorPane(600,400)。这就是我的启动方法的样子。

@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("loginpage.fxml"));
    primaryStage.setTitle("Eye Ratina Scanner");
    primaryStage.setScene(new Scene(root, 600, 400));
    primaryStage.show();
}

这就是我改变看法的方式。

AnchorPane pane = FXMLLoader.load(getClass().getResource("../loginpage.fxml"));
        dashpane.getChildren().setAll(pane) //dashpane is AnchorPane

我将整个应用程序的大小设为 600:400。现在我希望当我单击 window 最大化按钮时,所有组件的大小都会调整得更大并保持比例不变。下面附上两张图片以显示正在发生的事情。when i click on maximize

normal 600:400 view

现在我不想绑定每个按钮、图像视图、锚定窗格、文本字段、标签等。这将花费太长时间,因为我有大约 42 个 fxml 文件。我们可以通过一个片段来实现我们的结果吗?我没有发现任何其他 post 有完全相同的问题。

感谢您提前回答。非常感谢您的帮助。

这是一个示例布局。它使用 VBox 作为根。它使用 AnchorPaneAnchorPane里面是一个GridPane。您仍然需要努力使 ImageViewLabel 文本随着 Stage 变大而变大。

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

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.shape.Circle?>
<?import javafx.scene.text.Font?>


<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: lightblue;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <HBox maxHeight="400.0" prefHeight="75.0" VBox.vgrow="SOMETIMES">
         <children>
            <Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" text="Dashboard" HBox.hgrow="ALWAYS">
               <font>
                  <Font name="System Bold" size="19.0" />
               </font>
            </Label>
            <Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" text="Analytics" HBox.hgrow="ALWAYS">
               <font>
                  <Font name="System Bold" size="19.0" />
               </font>
            </Label>
            <Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" text="Settings" HBox.hgrow="ALWAYS">
               <font>
                  <Font name="System Bold" size="19.0" />
               </font>
            </Label>
            <Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" text="Notifications" HBox.hgrow="ALWAYS">
               <font>
                  <Font name="System Bold" size="19.0" />
               </font>
            </Label>
            <Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" text="Logout" HBox.hgrow="ALWAYS">
               <font>
                  <Font name="System Bold" size="19.0" />
               </font>
            </Label>
         </children>
      </HBox>
      <AnchorPane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: grey;" VBox.vgrow="ALWAYS">
         <children>
            <GridPane hgap="20.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" vgap="20.0" AnchorPane.bottomAnchor="20.0" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="20.0">
              <columnConstraints>
                  <ColumnConstraints hgrow="ALWAYS" maxWidth="1.7976931348623157E308" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="ALWAYS" maxWidth="1.7976931348623157E308" minWidth="10.0" prefWidth="100.0" />
                <ColumnConstraints hgrow="ALWAYS" maxWidth="1.7976931348623157E308" minWidth="10.0" prefWidth="100.0" />
                <ColumnConstraints hgrow="ALWAYS" maxWidth="1.7976931348623157E308" minWidth="10.0" prefWidth="100.0" />
              </columnConstraints>
              <rowConstraints>
                <RowConstraints maxHeight="1.7976931348623157E308" minHeight="10.0" prefHeight="30.0" vgrow="ALWAYS" />
                  <RowConstraints maxHeight="1.7976931348623157E308" minHeight="10.0" prefHeight="30.0" vgrow="ALWAYS" />
              </rowConstraints>
               <children>
                  <VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" style="-fx-background-color: white;">
                     <children>
                        <Circle fill="DODGERBLUE" radius="25.0" stroke="BLACK" strokeType="INSIDE" VBox.vgrow="ALWAYS" />
                        <Label text="Label" />
                        <Label text="Label" />
                     </children>
                  </VBox>
                  <VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" style="-fx-background-color: white;" GridPane.columnIndex="1">
                     <children>
                        <Circle fill="DODGERBLUE" radius="25.0" stroke="BLACK" strokeType="INSIDE" />
                        <Label text="Label" />
                        <Label text="Label" />
                     </children>
                  </VBox>
                  <VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" style="-fx-background-color: white;" GridPane.columnIndex="3" GridPane.rowIndex="1">
                     <children>
                        <Circle fill="DODGERBLUE" radius="40.0" stroke="BLACK" strokeType="INSIDE" />
                     </children>
                  </VBox>
                  <VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" style="-fx-background-color: white;" GridPane.columnIndex="2" GridPane.rowIndex="1">
                     <children>
                        <Circle fill="DODGERBLUE" radius="25.0" stroke="BLACK" strokeType="INSIDE" />
                        <Label text="Label" />
                        <Label text="Label" />
                     </children>
                  </VBox>
                  <VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" style="-fx-background-color: white;" GridPane.columnIndex="1" GridPane.rowIndex="1">
                     <children>
                        <Circle fill="DODGERBLUE" radius="25.0" stroke="BLACK" strokeType="INSIDE" />
                        <Label text="Label" />
                        <Label text="Label" />
                     </children>
                  </VBox>
                  <VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" style="-fx-background-color: white;" GridPane.rowIndex="1">
                     <children>
                        <Circle fill="DODGERBLUE" radius="25.0" stroke="BLACK" strokeType="INSIDE" />
                        <Label text="Label" />
                        <Label text="Label" />
                     </children>
                  </VBox>
                  <VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" style="-fx-background-color: white;" GridPane.columnIndex="3">
                     <children>
                        <Circle fill="DODGERBLUE" radius="25.0" stroke="BLACK" strokeType="INSIDE" />
                        <Label text="Label" />
                        <Label text="Label" />
                     </children>
                  </VBox>
                  <VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" style="-fx-background-color: white;" GridPane.columnIndex="2">
                     <children>
                        <Circle fill="DODGERBLUE" radius="25.0" stroke="BLACK" strokeType="INSIDE" />
                        <Label text="Label" />
                        <Label text="Label" />
                     </children>
                  </VBox>
               </children>
            </GridPane>
         </children>
      </AnchorPane>
   </children>
</VBox>