在一个场景中加载两个不同的FXML文件

时间:2019-04-11 09:28:03

标签: java javafx scenebuilder

我有两个不同的FXML文件 ViewOne.fxml -> ViewOneController ViewTwo.fxml -> ViewTwoController.java

在第一个视图上只有3个文本字段,在第二个视图上应根据第一个视图的输入画一个圆。

我的问题是如何在同一个场景中加载两个FXML文件,然后在以后使用事件绘制一个圆?

ViewOneController.java

public class ViewOneController {

    @FXML
    private HBox rootHBox;
    @FXML
    private TextField firstTextField;
    @FXML
    private TextField thirdTextField;
    @FXML
    private TextField secondTextField;

    @FXML
    void handleFirstTextField(ActionEvent event) {
    }

    @FXML
    void handleSecondTextField(ActionEvent event) {
    }

    @FXML
    void handleThirdTextField(ActionEvent event) {
    }
}

ViewOne.fxml

<HBox fx:id="rootHBox" maxHeight="-Infinity" maxWidth="-Infinity"   minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1"   fx:controller="exercise3Circles.ViewOneController">
   <children>
      <VBox prefHeight="400.0" prefWidth="243.0">
         <children>
            <AnchorPane prefHeight="401.0" prefWidth="243.0">
               <children>
                  <Label alignment="CENTER" layoutX="82.0" layoutY="44.0" text="View 1" textAlignment="CENTER">
                     <font>
                        <Font name="System Bold" size="20.0" />
                     </font>
                  </Label>
                  <Label layoutX="17.0" layoutY="120.0" text="X:" />
                  <TextField fx:id="firstTextField" layoutX="51.0" layoutY="115.0" onAction="#handleFirstTextField" />
                  <Label layoutX="17.0" layoutY="206.0" text="Y:" />
                  <TextField fx:id="thirdTextField" layoutX="51.0" layoutY="297.0" onAction="#handleThirdTextField" />
                  <Label layoutX="17.0" layoutY="302.0" text="R:" />
                  <TextField fx:id="secondTextField" layoutX="51.0" layoutY="201.0" onAction="#handleSecondTextField" />
               </children>
            </AnchorPane>
         </children>
      </VBox>
   </children>
</HBox>

ViewTwoController.java

public class ViewTwoController {

    @FXML
    private Label xLabel;

    @FXML
    private Label yLabel;

    @FXML
    private Label radiusLabel;
}

ViewTwo.fxml

<HBox alignment="CENTER_RIGHT" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="exercise3Circles.ViewTwoController">
   <children>
      <VBox alignment="CENTER_RIGHT" prefHeight="400.0" prefWidth="340.0">
         <children>
            <AnchorPane prefHeight="400.0" prefWidth="238.0">
           <children>
                  <Label layoutX="132.0" layoutY="44.0" text="View 2" textAlignment="CENTER">
                     <font>
                        <Font name="System Bold" size="20.0" />
                     </font>
                  </Label>
                  <Label layoutX="76.0" layoutY="356.0" text="X = 0" fx:id="xLabel" />
                  <Label fx:id="yLabel" layoutX="141.0" layoutY="356.0" text="Y = 0" />
                  <Label fx:id="radiusLabel" layoutX="208.0" layoutY="356.0" text="Radius = 0" />
               </children>
            </AnchorPane>
         </children>
      </VBox>
   </children>
</HBox>

0 个答案:

没有答案