java - 如何使辅助 anchor 板扩展其尺寸以适合主 anchor 板?

标签 java javafx javafx-8 fxml

我不知道如何将另一个 View 中的辅助 Anchorpane 设置到主 View 的边界(在 fxml 代码中,它将是名为 holderPane 并且有灰色区域),这样当窗口展开时,它也会随之拉伸(stretch)。我通过 SceneBuilder 完成所有工作。

这是在我最大化窗口之后:

My GettinStart view, which includes another view - StoksView

灰色区域就是那个Anchorpane,我尝试将另一个锚定区域绑定(bind)/设置到它。在 GettingStart.fxml 主 Controller 的代码中,我写的是:

@FXML
    private AnchorPane holderPane;

    private void setNode(Node node) {
        holderPane.getChildren().clear();
        holderPane.getChildren().add((Node) node);
    }
    @FXML
    public void initialize() {

        try {

            AnchorPane marketPane = FXMLLoader.load(getClass().getResource("../view/StocksView.fxml"));

            for (Node node:sidePane.getChildrenUnmodifiable()) {
                System.out.println(node.getAccessibleText());
            }

            for (Node node : sidePane.getChildren()) {
                if (node.getAccessibleText() != null) {
                    node.addEventHandler(MouseEvent.MOUSE_PRESSED, (MouseEvent ev) -> {
                        switch (node.getAccessibleText()) {
                            case "storesMenu":
                                setNode(marketPane);
                                txtCurrentWindow.setText("Stores");
                                break;

GettingStart.fxml

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

<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXToolbar?>
<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIconView?>
<?import de.jensd.fx.glyphs.octicons.OctIconView?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>

<AnchorPane prefHeight="600.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.lexkom.controll.StartwindowController">
   <children>
      <AnchorPane fx:id="sidePane" prefHeight="600.0" prefWidth="210.0" style="-fx-background-color: #363d49;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0">
         <children>
            <Label alignment="BASELINE_LEFT" layoutX="31.0" layoutY="102.0" prefHeight="31.0" prefWidth="136.0" text="Navigation" textFill="WHITE">
               <font>
                  <Font size="14.0" />
               </font>
            </Label>
            <Pane prefHeight="71.0" prefWidth="210.0" style="-fx-background-color: #363d49;" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
               <children>
                  <Label layoutX="21.0" layoutY="18.0" prefHeight="31.0" prefWidth="165.0" text="MyDimplomaProject" textFill="WHITE">
                     <font>
                        <Font size="16.0" />
                     </font>
                  </Label>
               </children>
            </Pane>
            <JFXButton fx:id="store" accessibleText="storesMenu" alignment="BASELINE_LEFT" graphicTextGap="17.0" layoutX="15.0" layoutY="152.0" prefHeight="50.0" prefWidth="180.0" ripplerFill="WHITE" text="Stores" textFill="WHITE">
               <font>
                  <Font size="18.0" />
               </font>
               <graphic>
                  <OctIconView fill="WHITE" glyphName="DATABASE" size="17" />
               </graphic>
            </JFXButton>
            <JFXButton fx:id="currency" accessibleText="currancyMenu" alignment="BASELINE_LEFT" graphicTextGap="17.0" layoutX="15.0" layoutY="200.0" prefHeight="50.0" prefWidth="180.0" ripplerFill="WHITE" text="Currancy Rates" textFill="WHITE">
               <font>
                  <Font size="18.0" />
               </font>
               <graphic>
                  <MaterialDesignIconView fill="WHITE" glyphName="CHART_AREASPLINE" size="17" />
               </graphic>
            </JFXButton>
            <Separator layoutX="15.0" layoutY="345.0" prefHeight="0.0" prefWidth="200.0" AnchorPane.leftAnchor="15.0" AnchorPane.rightAnchor="15.0" AnchorPane.topAnchor="345.0" />
            <Pane layoutX="15.0" layoutY="365.0" prefHeight="50.0" prefWidth="200.0" AnchorPane.leftAnchor="15.0" AnchorPane.rightAnchor="15.0">
               <children>
                  <ImageView fitHeight="50.0" fitWidth="51.0" layoutX="1.0" layoutY="1.0" pickOnBounds="true" preserveRatio="true">
                     <image>
                        <Image url="@../resources/User_Avatar-64.png" />
                     </image>
                  </ImageView>
                  <Label layoutX="53.0" layoutY="6.0" text="User name" textFill="WHITE">
                     <font>
                        <Font size="16.0" />
                     </font>
                  </Label>
               </children>
            </Pane>
            <JFXButton fx:id="logoutBtn" alignment="BASELINE_LEFT" graphicTextGap="17.0" layoutX="15.0" layoutY="435.0" onAction="#logOut" prefHeight="50.0" prefWidth="180.0" ripplerFill="WHITE" text="Log Out" textFill="WHITE">
               <font>
                  <Font size="18.0" />
               </font>
               <graphic>
                  <FontAwesomeIconView fill="WHITE" glyphName="SIGN_OUT" size="17" />
               </graphic>
            </JFXButton>
            <JFXButton alignment="BASELINE_LEFT" graphicTextGap="17.0" layoutX="15.0" layoutY="485.0" onAction="#closelabel" prefHeight="50.0" prefWidth="180.0" ripplerFill="WHITE" text="Exit" textFill="WHITE">
               <font>
                  <Font size="18.0" />
               </font>
               <graphic>
                  <FontAwesomeIconView fill="WHITE" glyphName="POWER_OFF" size="17" />
               </graphic>
            </JFXButton>
            <Label graphicTextGap="16.0" layoutX="30.0" layoutY="560.0" text="About" textFill="WHITE" />
         </children>
      </AnchorPane>
      <JFXToolbar fx:id="window" layoutX="210.0" prefHeight="48.0" prefWidth="791.0" style="-fx-background-color: #363d49;" AnchorPane.leftAnchor="209.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <center>
            <Label fx:id="txtCurrentWindow" textFill="WHITE" BorderPane.alignment="CENTER">
               <font>
                  <Font name="System Bold" size="22.0" />
               </font>
            </Label>
         </center>
         <right>
            <HBox prefHeight="48.0" prefWidth="129.0" BorderPane.alignment="CENTER">
               <children>
                  <Pane prefHeight="200.0" prefWidth="60.0">
                     <children>
                        <MaterialDesignIconView fill="WHITE" glyphName="WINDOW_MINIMIZE" layoutX="12.0" layoutY="34.0" onMousePressed="#minimizeStage" size="16" />
                     </children>
                  </Pane>
                  <Pane prefHeight="200.0" prefWidth="60.0">
                     <children>
                        <MaterialDesignIconView fill="WHITE" glyphName="WINDOW_MAXIMIZE" layoutX="12.0" layoutY="31.0" onMousePressed="#maximizeStage" size="16" />
                     </children>
                  </Pane>
                  <Pane prefHeight="200.0" prefWidth="60.0">
                     <children>
                        <MaterialDesignIconView fill="WHITE" glyphName="WINDOW_CLOSE" layoutX="9.0" layoutY="31.0" onMouseClicked="#closelabel" onMousePressed="#closelabel" size="16" text="" />
                     </children>
                  </Pane>
               </children>
            </HBox>
         </right>
      </JFXToolbar>
      <AnchorPane fx:id="holderPane" layoutX="215.0" layoutY="54.0" opacity="0.8" prefHeight="540.0" prefWidth="780.0" style="-fx-background-color: #9a9b9d;" AnchorPane.bottomAnchor="4.0" AnchorPane.leftAnchor="215.0" AnchorPane.rightAnchor="4.0" AnchorPane.topAnchor="54.0" />
   </children>
</AnchorPane>

其中 StocksView.fxml 具有正常形式的灰色区域的尺寸。

StocksView.fxml

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

<?import com.jfoenix.controls.JFXTabPane?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="540.0" prefWidth="780.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <JFXTabPane layoutX="323.0" layoutY="191.0" prefHeight="540.0" prefWidth="780.0" style="-fx-background-color: #fff;" tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <tabs>
            <Tab closable="false" text="Manage Sheets">
              <content>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
              </content>
            </Tab>
            <Tab closable="false" text="Store">
               <content>
                  <fx:include source="TabStockView.fxml" />
               </content></Tab>
         </tabs>
         <effect>
            <DropShadow />
         </effect>
      </JFXTabPane>
   </children>
</AnchorPane>

最佳答案

只需将 anchor 设置为 0 即可使父级 AnchorPane 调整子级的大小以填充其整个区域:

private void setNode(Node node) {
    AnchorPane.setBottomAnchor(node, 0);
    AnchorPane.setTopAnchor(node, 0);
    AnchorPane.setLeftAnchor(node, 0);
    AnchorPane.setRightAnchor(node, 0);

    holderPane.getChildren().setAll(node);
}

关于java - 如何使辅助 anchor 板扩展其尺寸以适合主 anchor 板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47033107/

相关文章:

尽管已安装,JavaFX PieChart 工具提示仍不显示

JavaFx 与 Maven : doesn't find the mainClass

java - 数据库池

java.io.IOException : Cannot run program "": CreateProcess error=2, 系统找不到指定的文件

java - QueryDsl 不会为 git 子模块的 @Entity 类生成 q 类

java - 扩展应用程序的扩展抛出 InvocationTargetException

Javamail 和 Javafx。读取和显示邮件嵌入图像

java - 包含包装标签的控制皮肤的首选高度

Java RenderedImage 小节

java - Java Applet允许Javac或其他jar访问吗?