提问者:小点点

JavaFX:选项卡内的锚窗格约束


我有一个带有多个fxml文件的JavaFX应用程序,MainPanel设置如下:

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

<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import java.net.URL?>

<AnchorPane xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MainPanelController">
   <children>
      <TabPane fx:id="mainTabPane" prefHeight="570.0" prefWidth="880.0" tabClosingPolicy="UNAVAILABLE" tabMaxHeight="20.0" tabMaxWidth="3520.0" tabMinHeight="20.0" tabMinWidth="50.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
            <tabs>
                <Tab fx:id="tab1" text="Tab 1" />
                <Tab fx:id="tab2" text="Tab 2" />
                <Tab fx:id="tab3" text="Tab 3" />
                <Tab fx:id="tab4" text="Tab 4" />
                <Tab fx:id="tab5" text="Tab 5" />
                <Tab fx:id="tab6" text="Tab 6" />
                <Tab fx:id="tab7" text="Tab 7" />
            </tabs>
        </TabPane>
   </children>
</AnchorPane>

过程如下:

>

  • 该应用程序由以下人员启动:

    Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
    primaryStage.setScene(new Scene(root, 880, 570));
    primaryStage.setTitle("Login");
    primaryStage.show();
    

    登录fxml为:

     <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.control.Button?>
    <?import javafx.scene.control.Label?>
    <?import javafx.scene.control.PasswordField?>
    <?import javafx.scene.control.TextField?>
    <?import javafx.scene.layout.AnchorPane?>
    <?import javafx.scene.layout.VBox?>
    <?import javafx.scene.text.Font?>
    
    
    <AnchorPane fx:id="mainAnchorPane" prefHeight="550.0" prefWidth="880.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="LoginController">
       <children>
          <VBox layoutX="340.0" layoutY="186.0" spacing="5.0" AnchorPane.bottomAnchor="186.0" AnchorPane.leftAnchor="340.0" AnchorPane.rightAnchor="340.0" AnchorPane.topAnchor="186.0">
             <children>
                  <Label alignment="CENTER" prefHeight="73.0" prefWidth="200.0" text="GM-SIS">
                      <font>
                          <Font name="Helvetica" size="60.0" />
                      </font>
                  </Label>
                  <TextField fx:id="userField" prefHeight="30.0" prefWidth="200.0" />
                  <PasswordField fx:id="passwordField" prefHeight="30.0" prefWidth="200.0" />
                  <Button fx:id="loginButton" mnemonicParsing="false" onAction="#login" prefHeight="30.0" prefWidth="200.0" text="Login">
                      <font>
                          <Font name="Helvetica" size="13.0" />
                      </font>
                  </Button>
             </children>
          </VBox>
            <Label alignment="CENTER" layoutX="270.0" layoutY="265.0" prefHeight="30.0" prefWidth="60.0" text="User">
             <font>
                <Font name="Helvetica" size="13.0" />
             </font>
          </Label>
            <Label alignment="CENTER" layoutX="270.0" layoutY="300.0" prefHeight="30.0" prefWidth="60.0" text="Password" />
       </children>
    </AnchorPane>
    

    用户点击登录按钮,主面板如下加载:

    Node mainNode = FXMLLoader.load(Login.class.getResource(fxmlPath));
    mainAnchorPane.getChildren().setAll(mainNode);
    

    然后,主面板控制器以类似的方式将每个单独的fxml加载到每个选项卡中。我试过这样的方法:

    tab1.setContent(loadedNode);
    

    还有这个:

    tab1AnchorPane.getChildren().setAll(tab1Node);
    

    我尝试过让选项卡没有子项,只加载到其中。我尝试过在选项卡中设置AnchorPane。我尝试了将HBoxe/VBoxes与AnchorPanes组合使用,但没有成功。

    如果我单独加载fxml文件,AnchorPane约束有效。但是,当我尝试将其加载到选项卡中时,它不起作用。

    这是一个fxml,如果单独加载,它可以工作,而加载到选项卡时则不行:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.geometry.Insets?>
    <?import javafx.scene.control.Button?>
    <?import javafx.scene.control.TableColumn?>
    <?import javafx.scene.control.TableView?>
    <?import javafx.scene.layout.AnchorPane?>
    <?import javafx.scene.layout.HBox?>
    <?import javafx.scene.layout.VBox?>
    
    
    <AnchorPane fx:id="tab1AnchorPane" prefHeight="550.0" prefWidth="880.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Tab1Controller">
        <children>
            <VBox prefHeight="550.0" prefWidth="880.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                <children>
                    <TableView fx:id="table" editable="true" prefHeight="450.0" prefWidth="474.0" VBox.vgrow="ALWAYS">
                        <columns>
                            <TableColumn fx:id="column1" prefWidth="75.0" text="Column 1" />
                            <TableColumn fx:id="column2" prefWidth="75.0" text="Column 2" />
                        </columns>
                        <VBox.margin>
                            <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                        </VBox.margin>
                    </TableView>
                    <HBox alignment="CENTER" prefHeight="30.0" prefWidth="880.0" spacing="50.0" VBox.vgrow="ALWAYS">
                        <children>
                            <VBox spacing="50.0">
                                <children>
                                    <Button fx:id="button1" mnemonicParsing="false" onAction="#action1" prefHeight="30.0" prefWidth="150.0" text="Button 1" />
                                    <Button fx:id="button2" mnemonicParsing="false" onAction="#action2" prefHeight="30.0" prefWidth="150.0" text="Button 2" />
                                </children>
                            </VBox>
                            <VBox spacing="10.0">
                                <children>
                                    <Button fx:id="button3" mnemonicParsing="false" onAction="#action3" prefHeight="30.0" prefWidth="150.0" text="Button 3" />
                                    <Button fx:id="button4" mnemonicParsing="false" onAction="#action4" prefHeight="30.0" prefWidth="150.0" text="Button 4" />
                                    <Button fx:id="button5" mnemonicParsing="false" onAction="#action5" prefHeight="30.0" prefWidth="150.0" text="Button 5" />
                                </children>
                            </VBox>
                            <VBox spacing="10.0">
                                <children>
                                    <Button fx:id="button6" mnemonicParsing="false" onAction="#action6" prefHeight="30.0" prefWidth="150.0" text="Button 6" />
                                    <Button fx:id="button7" mnemonicParsing="false" onAction="#action7" prefHeight="30.0" prefWidth="150.0" text="Button 7" />
                                    <Button fx:id="button8" mnemonicParsing="false" onAction="#action8" prefHeight="30.0" prefWidth="150.0" text="Button 8" />
                                </children>
                            </VBox>
                            <VBox spacing="50.0">
                                <children>
                                    <Button fx:id="button9" maxWidth="150.0" mnemonicParsing="false" onAction="#" prefHeight="30.0" text="Button 9" />
                                    <Button fx:id="button10" mnemonicParsing="false" onAction="#action9" prefHeight="30.0" prefWidth="150.0" text="Button 10" />
                                </children>
                            </VBox>
                        </children>
                        <VBox.margin>
                            <Insets bottom="20.0" left="10.0" right="10.0" top="10.0" />
                        </VBox.margin>
                    </HBox>
                </children>
            </VBox>
        </children>
    </AnchorPane>
    

    甚至主面板内的TabPane也无法正确调整大小。我在这里做错了什么?我感到有点迷茫。


  • 共1个答案

    匿名用户

    我也有你提到的类似情况。

    当在 fxml 加载的根窗格上添加某个节点时,根窗格根本不会调整与阶段对应的大小。(我怀疑它可能在JavaFx上出错。

    因此,我在stage的widthProperty(或heightProperty)上添加了侦听器,以同时调整stage的根窗格的大小。

        Scene      scene = stage.getScene();
        AnchorPane root  = (AnchorPane) scene.getRoot();
        scene.widthProperty().addListener( ( observable, oldValue, newValue ) -> root.setPrefWidth( newValue.doubleValue() ) );
        scene.heightProperty().addListener( ( observable, oldValue, newValue ) -> root.setPrefHeight( newValue.doubleValue() ) );