我正在使用 JavaFX 2.2 开发桌面应用程序。我有一个加载主 fxml 的应用程序类。在那个主 fxml 中,我有一个边框窗格,在中间部分,我包含了一个子 fxml,使用<fx:include>
. 我的问题是,子 fxml 的控制器初始化方法在父控制器之前被调用。我需要在应用程序类中加载父级时对其进行初始化。我不知道它是实际的还是行为,或者我需要做其他事情才能完成它。我对 JavaFX 真的很陌生。
下面是我的示例代码。
//主应用类
public class DemoApplication extends Application {
static double stage_width, stage_height;
@Override
public void start(Stage stage) throws Exception {
Rectangle2D primScreenBounds = Screen.getPrimary().getVisualBounds();
stage_width = primScreenBounds.getWidth();
stage_height = primScreenBounds.getHeight();
Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));
stage.setX(0);
stage.setY(0);
stage.setWidth(primScreenBounds.getWidth());
stage.setHeight(primScreenBounds.getHeight());
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
//主控制器
public class MainController implements Initializable {
@Override
public void initialize(URL url, ResourceBundle rb) {
System.out.println("Initializing the parent controller");
}
}
//子控制器
public class ChildController implements Initializable {
@Override
public void initialize(URL url, ResourceBundle rb) {
System.out.println("Initializing the child controller");
}
}
//主文件
<AnchorPane id="AnchorPane" fx:id="ap_screen" prefHeight="821.0000999999975" prefWidth="1395.0" xmlns:fx="http://javafx.com/fxml" fx:controller="com.demo.app.controller.MainController">
<children>
<BorderPane fx:id="bp" prefHeight="754.0" prefWidth="1282.0">
<center>
<Pane fx:id="p2_cen" prefHeight="200.0" prefWidth="200.0">
<children>
<HBox fx:id="p1_cen" prefHeight="100.0" prefWidth="523.0" styleClass="hb">
<children>
<Button mnemonicParsing="false" prefHeight="20.0" prefWidth="100.0" text="Favourite" textFill="WHITE" HBox.margin="$x2" />
</children>
<stylesheets>
<URL value="@styles.css" />
</stylesheets>
</HBox>
**<fx:include source="child.fxml" layoutY="100.0" />**
</children>
</Pane>
</center>
<left>
<VBox fx:id="vb_left" prefHeight="419.0" prefWidth="200.0" styleClass="vbox">
<children>
<ComboBox fx:id="combo" onAction="#combo" prefHeight="25.0" prefWidth="200.0" styleClass="combo">
<stylesheets>
<URL value="@styles.css" />
</stylesheets>
</ComboBox>
</children>
<stylesheets>
<URL value="@styles.css" />
</stylesheets>
</VBox>
</left>
<top>
<HBox fx:id="hb_top" prefHeight="100.0" prefWidth="500.0" styleClass="hbox">
</HBox>
</top>
</BorderPane>
</children>
</AnchorPane>
//子fxml
<AnchorPane id="AnchorPane" prefHeight="457.0" prefWidth="619.0" xmlns:fx="http://javafx.com/fxml" fx:controller="com.demo.app.controller.ChildController">
<children>
<ScrollPane fx:id="sp1" fitToWidth="true" layoutY="189.0" prefHeight="200.0" prefWidth="364.0">
<content>
<AnchorPane id="Content" prefHeight="241.0" prefWidth="435.0">
<children>
<VBox fx:id="vbmain" prefHeight="100.0" prefWidth="174.0" />
<Button id="b1" fx:id="b2" layoutY="40.0" mnemonicParsing="false" onAction="#movleft" prefHeight="80.0" prefWidth="40.0" text="Button" />
<Button id="b2" fx:id="b1" layoutX="246.0" layoutY="40.0" mnemonicParsing="false" onAction="#movright" prefHeight="80.0" prefWidth="40.0" text="Button" />
</children>
</AnchorPane>
</content>
</ScrollPane>
</children>
</AnchorPane>