在AnchorPane中不调整AnchorPane的大小

时间:2018-10-12 21:09:01

标签: java javafx javafx-2 fxml

我正在编写一个JavaFX应用程序,在该程序的顶部有一个菜单栏,在其下方有一个AnchorPane,可以在其中使用其他FXML文件显示内容。问题是当代码运行时,即使将Vgrow设置为“ Always”,其他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。

2 个答案:

答案 0 :(得分:1)

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

替换父布局的子列表中的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覆盖,而不会以任何方式影响布局。

答案 1 :(得分:0)

出于调整大小的目的,我会避免使用AnchorPanes。通常,如果您要维护的区域的尺寸为静态,而无论窗口大小如何,它们的效果都会更好。用BorderPanes或FlowPanes替换AnchorPanes容易得多。通常,这将解决许多调整大小的问题。