JavaFx Controller初始化无法正常工作

时间:2013-05-17 10:10:59

标签: java javafx fxml

我正在使用JavaFX 2.2开发桌面应用程序。我有一个加载主fxml的应用程序类。在那个主要的fxml我有一个Border窗格,在中间部分,我使用<fx:include>包含了一个子fxml。我的问题是,在父控制器之前调用子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");
    }
}

// Main 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>

// child 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>

1 个答案:

答案 0 :(得分:0)

实际上,孩子在父母之前被实例化:要正确地布置父母FXML,必须首先处理孩子(绘制父母意味着先画儿童)。

如果你想从修改其子代的父组件维护某种状态,你可以将状态放在一个单独的状态,你可以随时在父代和子代中使用它,有很多例子Spring与JavaFX集成。

相关问题