【问题标题】:No resizing of AnchorPane inside an AnchorPane没有在 AnchorPane 内调整 AnchorPane 的大小
【发布时间】:2018-10-12 21:09:01
【问题描述】:

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

有人可以告诉我该怎么做才能让内部 AnchorPane 获得除标题栏之外的所有空间...

下面是父 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>

下面是子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类的代码(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
}

关注的方法: SavingCreateDeleteAccountOnAction() Main.primaryStage : Main 类中的静态变量,用于存储程序的 primaryStage。

【问题讨论】:

    标签: java javafx javafx-2 fxml


    【解决方案1】:

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

    在父布局的子列表中替换 displayPane 将允许 VBox 调整大小/位置。


    在这种情况下使用不同的布局要简单得多: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);
    }
    

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

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

    没有任何作用。

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-10
        • 2018-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-09
        • 2021-06-06
        相关资源
        最近更新 更多