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>

过程是这样的:

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

    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>
  1. 用户单击登录按钮,主面板加载如下:

    Node mainNode = FXMLLoader.load(Login.class.getResource(fxmlPath));
    mainAnchorPane.getChildren().setAll(mainNode);
    
  2. 然后,主面板控制器以类似的方式将每个单独的 fxml 加载到每个选项卡中。我试过这样的事情:

    tab1.setContent(loadedNode);
    

而这个:

tab1AnchorPane.getChildren().setAll(tab1Node);

我尝试过让没有孩子的选项卡,只是加载到其中。我试过在选项卡中有一个锚窗格。我尝试过HBoxes/VBoxes与AnchorPanes的组合,但没有成功。

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

这是一个 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 也无法正确调整大小。我在这里做错了什么?我感到有点失落。

我也遇到了你提到的类似情况。

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

因此,我在舞台的宽度属性(或高度属性)上添加了侦听器,以同时调整舞台的根窗格的大小。

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() ) );

最新更新