多个FXML文件以及每个文件的控制器-追加后TextArea无法正确显示文本

时间:2019-02-25 10:15:55

标签: java javafx fxml

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

TextArea

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

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

alert.setContentText(textAreaSample.getText());

及其控制器:

<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); } } 中定义了ControllerSending的getter和setter方法?然后在ControllerSample中,我用吸气剂和吸气剂定义了ControllerSending?然后在Main中声明ControllerSendingPhoto我使用ControllerSendingPhotos吗?

1 个答案:

答案 0 :(得分:2)

要访问嵌套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);
    }
}