0

在我的应用程序中,我正在切换 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 大小的整个应用程序。现在我希望当我单击窗口最大化按钮时,所有组件都会调整大小并保持比例。下面附有两张图片以显示正在发生的事情。当我点击最大化

正常 600:400 视图

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

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

4

1 回答 1

0

这是一个示例布局。它使用 aVBox作为根。它使用一个AnchorPane. 里面AnchorPane是一个GridPane。你仍然需要做一些工作来使文本随着变大ImageView而增长。LabelStage

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

在此处输入图像描述

在此处输入图像描述

于 2020-07-01T14:28:53.340 回答