AnchorPane 内不调整 AnchorPane 的大小

No resizing of AnchorPane inside an AnchorPane

我正在编写一个 JavaFX 应用程序,其中顶部有一个菜单栏,下方有一个 AnchorPane,我可以在其中使用其他 FXML 文件来显示内容。问题是当代码运行时,即使我将 Vgrow 设置为 "Always",其他 FXML 文件的内容也不会调整大小。所有这些都在 AnchorPane 中。

有人可以告诉我如何获得内部 AnchorPane 所有 space 除了标题栏之外...

下面是 parent AnchorPane (savingsCreateDeleteAccount.fxml) 的代码:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>

<AnchorPane prefHeight="455.0" prefWidth="753.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="HomePage">
   <children>
      <VBox layoutX="-122.0" layoutY="41.0" prefHeight="200.0" prefWidth="722.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <children>
            <MenuBar>
              <menus>
                <Menu mnemonicParsing="false" text="Home">
                  <items>
                        <MenuItem mnemonicParsing="false" onAction="#homeScreenOnAction" text="HomeScreen" />
                    <MenuItem mnemonicParsing="false" onAction="#closeOnAction" text="Close" />
                  </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Account">
                     <items>
                        <Menu mnemonicParsing="false" text="Savings Account">
                          <items>
                            <MenuItem mnemonicParsing="false" onAction="#savingsCreateDeleteAccountOnAction" text="Create/Delete Account" />
                              <MenuItem mnemonicParsing="false" onAction="#savingsViewAccountsOnAction" text="View Account(s)" />
                          </items>
                        </Menu>
                        <Menu mnemonicParsing="false" text="Current Account">
                          <items>
                            <MenuItem mnemonicParsing="false" onAction="#currentCreateDeleteAccountOnAction" text="Create/Delete Account" />
                              <MenuItem mnemonicParsing="false" onAction="#currentViewAccountsOnAction" text="View Account(s)" />
                          </items>
                        </Menu>
                     </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Transactions">
                  <items>
                    <MenuItem mnemonicParsing="false" text="Personal Transactions" />
                        <MenuItem mnemonicParsing="false" text="Business Transactions" />
                  </items>
                </Menu>
                  <Menu mnemonicParsing="false" text="Loan">
                    <items>
                      <MenuItem mnemonicParsing="false" text="Student Loan" />
                        <MenuItem mnemonicParsing="false" text="Property Loan" />
                        <MenuItem mnemonicParsing="false" text="Business Loan" />
                    </items>
                  </Menu>
                  <Menu mnemonicParsing="false" text="Help">
                    <items>
                      <MenuItem mnemonicParsing="false" text="About Bank" />
                        <MenuItem mnemonicParsing="false" text="About App" />
                    </items>
                  </Menu>
              </menus>
            </MenuBar>
            <Pane fx:id="displayPane" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" VBox.vgrow="ALWAYS" />
         </children>
      </VBox>
   </children>
</AnchorPane>

下面是 child AnchorPane (homePage.fxml) 的代码:

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

<?import javafx.scene.layout.Pane?>


<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: red;" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" />

下面是Controller的代码Class (HomePage.java) :

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;

public class HomePage {

    @FXML Pane displayPane;

    @FXML public void homeScreenOnAction() throws Exception {
        Pane newPane = FXMLLoader.load(getClass().getResource("homePage.fxml"));
        Main.primaryStage.setScene(new Scene(newPane, 1000, 600));
        Main.primaryStage.show();
    }

    @FXML public void closeOnAction() {
        Main.primaryStage.close();
    }

    @FXML public void savingsCreateDeleteAccountOnAction() throws Exception {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("savingsCreateDeleteAccount.fxml"));
        Pane tempPane = fxmlLoader.load();
        displayPane.getChildren().setAll(tempPane);
    }

    @FXML public void savingsViewAccountsOnAction() {

    }

    @FXML public void currentCreateDeleteAccountOnAction() {

    }

    @FXML public void currentViewAccountsOnAction() {

    }

//    Transactions, Loan and Help
}

要关注的方法:savingsCreateDeleteAccountOnAction() Main.primaryStage : Main class 中的静态变量,用于存放程序的primaryStage。

问题是您将从 fxml 加载的内容添加到 Pane,除了将内容调整为首选大小外,它不执行任何定位或调整大小。

替换 parent 布局的 child 列表中的 displayPane 将允许 VBox 到 resize/position。


虽然在这种情况下使用不同的布局要简单得多:BorderPane

此布局允许您轻松替换 displayPane 之类的节点,还可以自动调整占据它的节点的大小:

<BorderPane fx:id="container" prefHeight="455.0" prefWidth="753.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="HomePage">
    <top>
        <MenuBar>
        ...
        </MenuBar>
    </top>
    <center>
        <Pane fx:id="displayPane" prefHeight="200.0" prefWidth="200.0" />
    </center>
</BorderPane>
@FXML
private BorderPane container;

@FXML private void savingsCreateDeleteAccountOnAction() throws Exception {
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("savingsCreateDeleteAccount.fxml"));
    Pane tempPane = fxmlLoader.load();
    container.setCenter(tempPane);
}

注意: 使用不是节点 parent 的布局的静态属性是没有意义的。例如。 displayPane 的 parent 是 VBox 所以

AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"

没有任何影响。

也手动设置layoutX/layoutY,如果parent布局做定位是没有意义的。 VBox 的那些属性在第一次布局过程中被 parent AnchorPane 简单地覆盖,而不会以任何方式影响布局。

出于调整大小的目的,我会避免使用 AnchorPanes。通常,如果您要维护的区域具有静态大小,无论 window 大小如何,它们都会更好地工作。用 BorderPanes 或 FlowPanes 替换 AnchorPanes 要容易得多。这通常会解决许多大小调整问题。