【发布时间】:2017-03-28 22:20:56
【问题描述】:
我有一个包含多个 fxml 文件的 JavaFX 应用程序,主面板设置如下:
<?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);
我尝试过没有子级的选项卡,只是加载到其中。我尝试在选项卡内放置一个 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 都无法正确调整大小。我在这里做错了什么?我有点失落。
【问题讨论】:
-
需要做很多工作才能让您重新考虑所提供的内容。你在 GitHub 上有这个项目吗?
-
@SedrickJefferson 不幸的是,我不能真正分享整个项目。希望这是我的一些简单错误。其中之一是通过单线解决的。