我有一个带有多个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>
用户点击登录按钮,MainPanel按如下方式加载:
Node mainNode = FXMLLoader.load(Login.class.getResource(fxmlPath));
mainAnchorPane.getChildren().setAll(mainNode);
然后,MainPanel控制器以类似的方式将每个fxml加载到每个选项卡中。我尝试过这样的事情:
tab1.setContent(loadedNode);
而且:
tab1AnchorPane.getChildren().setAll(tab1Node);
我尝试过没有孩子的Tab,只是装入它。我已经尝试在标签中放置一个AnchorPane。我已尝试将HBoxes / 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>
即使MainPanel中的TabPane也没有正确调整大小。我在这做错了什么?我感觉有点迷失。