0

JavaFX 2.2 を使用してデスクトップ アプリケーションに取り組んでいます。メインの fxml をロードするアプリケーション クラスがあります。そのメインの fxml には Border ペインがあり、中央部分には<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");
    }
}

//メイン 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>
4

1 に答える 1

0

事実上、子は親の前にインスタンス化されます。親 FXML を正しくレイアウトするには、子を最初に処理する必要があります (親を描画すると、子が最初に描画されます)。

子を変更する親コンポーネントから何らかの状態を維持したい場合は、必要なときにいつでも親と子の両方で使用できるシングルトンに状態を配置できます。Spring との統合の例はたくさんあります。 JavaFX.

于 2013-09-19T22:39:52.030 に答える