多个 FXML 文件,每个文件都有控制器 - TextArea 在追加后无法正确显示文本

Multiple FXML files with controllers for each file - TextArea not displaying text properly after append

我想将我的 FXML 分成更小的文件,每个文件都有自己的控制器。在 main 中,我为每个控制器创建实例并访问 textAreaSample 并尝试附加文本。我没有看到文字正在改变。为什么? Alert 正在显示来自此 TextArea 的文本:

alert.setContentText(textAreaSample.getText());

我不知道如何设置所有的 fxml 文件和控制器。我应该如何设置所有这些?

这是我的主要“sample.fxml”文件:

<GridPane fx:controller="sample.ControllerSample"
      xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">

    <fx:include fx:id="sending" source="Sending.fxml" GridPane.columnIndex="0" GridPane.rowIndex="0"/>

    <TextArea fx:id="textAreaSample" prefWidth="200" prefHeight="200"
          GridPane.columnIndex="1" GridPane.rowIndex="0" text="Sample">
    </TextArea>
</GridPane>

及其控制者:

public class ControllerSample {

    @FXML
    private TextArea textAreaSample;

    public ControllerSample() {}

    public TextArea getTextAreaSample() {
        return textAreaSample;
    }

    public void setTextAreaSample(TextArea textAreaSample) {
        this.textAreaSample = textAreaSample;
    }
}

现在我有 Sending.fxml 个文件:

<GridPane fx:controller="sample.ControllerSending"
          xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">

        <fx:include fx:id="sendingPhotos" source="SendingPhotos.fxml" GridPane.columnIndex="0" GridPane.rowIndex="0"/>
</GridPane>

及其控制者:

public class ControllerSending {

    public ControllerSending() {}
}

这里是SendingPhotos.fxml代码:

<TextArea xmlns="http://javafx.com/javafx"
      xmlns:fx="http://javafx.com/fxml"
      fx:controller="sample.ControllerSendingPhotos" fx:id="textAreaSendingPhotos" prefWidth="200" prefHeight="200"
      text="Photos"/>

和控制器:

public class ControllerSendingPhotos {

    @FXML
    private TextArea textAreaSendingPhotos;

    public ControllerSendingPhotos() {}

    public TextArea getTextAreaSendingPhotos() {
        return textAreaSendingPhotos;
    }

    public void setTextAreaSendingPhotos(TextArea textAreaSendingPhotos) {
        this.textAreaSendingPhotos = textAreaSendingPhotos;
    }
}

现在Main.java代码:

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        FXMLLoader loaderSample = new FXMLLoader(getClass().getResource("sample.fxml"));
        FXMLLoader loaderSending = new FXMLLoader(getClass().getResource("Sending.fxml"));
        FXMLLoader loaderSendingPhotos = new FXMLLoader(getClass().getResource("SendingPhotos.fxml"));

        loaderSample.load();
        loaderSending.load();
        loaderSendingPhotos.load();

        ControllerSample controllerSample = (ControllerSample) loaderSample.getController();
        ControllerSending controllerSending = (ControllerSending) loaderSending.getController();
        ControllerSendingPhotos controllerSendingPhotos = (ControllerSendingPhotos) loaderSendingPhotos.getController();

        TextArea textAreaSample = controllerSample.getTextAreaSample();
        textAreaSample.setText("\ndebug textAreaSample\n");

        Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "");

        alert.setContentText(textAreaSample.getText());
        alert.showAndWait();

        //TextArea textAreaSendingPhotos = controllerSendingPhotos.getTextAreaSendingPhotos();
        //textAreaSendingPhotos.appendText("\ndebug textAreaSendingPhotos\n");

        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 800, 400));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

编辑:我在 ControllerSample 中用 getter 和 setter 定义了 ControllerSending?然后在 ControllerSending 中我用 getter 和 setter 定义了 ControllerSendingPhoto?然后在 Main 中声明 ControllerSendingPhotos 我使用 controllerSample.getControllerSending().getControllerSendingPhotos()?

要访问嵌套 fxml 中的 textArea,您必须更改控制器:

public class ControllerSample {
    @FXML
    private TextArea textAreaSample;

    @FXML
    private ControllerSending sendingController;

    public ControllerSample() {
    }

    public TextArea getTextAreaSample() {
        return textAreaSample;
    }

    public void setTextAreaSample(TextArea textAreaSample) {
        this.textAreaSample = textAreaSample;
    }

    protected ControllerSending getSendingController() {
        return sendingController;
    }
}

public class ControllerSending {
    @FXML
    private ControllerSendingPhotos sendingPhotosController;

    public ControllerSending() {
    }

    protected ControllerSendingPhotos getSendingPhotosController() {
        return sendingPhotosController;
    }
}

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        FXMLLoader loaderSample = new FXMLLoader(getClass().getResource("sample.fxml"));

        Parent root = loaderSample.load();

        ControllerSample controllerSample = (ControllerSample) loaderSample.getController();

        TextArea textAreaSample = controllerSample.getTextAreaSample();
        textAreaSample.setText("\ndebug textAreaSample\n");

        TextArea textAreaSendingPhotos = controllerSample.getSendingController().getSendingPhotosController()
            .getTextAreaSendingPhotos();
        textAreaSendingPhotos.setText("test test test");

        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 800, 400));
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}