0
我正在使用JavaFX 2.2的桌面應用程序。我有一個加載主要fxml的應用程序類。在那個主要的fxml中我有一個Border窗格,在中間部分,我包含了一個使用<fx:include>
的子fxml。我的問題是,在父控制器之前調用子fxml的控制器初始化方法。當它加載到應用程序類中時,我需要讓父對象初始化。我不知道它的實際或行爲,或者我需要做其他事情來完成它。我對JavaFX真的很陌生。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");
}
}
//主FXML
<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>