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

我想将我的 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;

    }

}


跃然一笑
浏览 180回答 1
1回答

慕田峪9158850

要访问textArea嵌套的 fxml,您必须更改控制器:public class ControllerSample {&nbsp; &nbsp; @FXML&nbsp; &nbsp; private TextArea textAreaSample;&nbsp; &nbsp; @FXML&nbsp; &nbsp; private ControllerSending sendingController;&nbsp; &nbsp; public ControllerSample() {&nbsp; &nbsp; }&nbsp; &nbsp; public TextArea getTextAreaSample() {&nbsp; &nbsp; &nbsp; &nbsp; return textAreaSample;&nbsp; &nbsp; }&nbsp; &nbsp; public void setTextAreaSample(TextArea textAreaSample) {&nbsp; &nbsp; &nbsp; &nbsp; this.textAreaSample = textAreaSample;&nbsp; &nbsp; }&nbsp; &nbsp; protected ControllerSending getSendingController() {&nbsp; &nbsp; &nbsp; &nbsp; return sendingController;&nbsp; &nbsp; }}public class ControllerSending {&nbsp; &nbsp; @FXML&nbsp; &nbsp; private ControllerSendingPhotos sendingPhotosController;&nbsp; &nbsp; public ControllerSending() {&nbsp; &nbsp; }&nbsp; &nbsp; protected ControllerSendingPhotos getSendingPhotosController() {&nbsp; &nbsp; &nbsp; &nbsp; return sendingPhotosController;&nbsp; &nbsp; }}public class Main extends Application {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void start(Stage primaryStage) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; FXMLLoader loaderSample = new FXMLLoader(getClass().getResource("sample.fxml"));&nbsp; &nbsp; &nbsp; &nbsp; Parent root = loaderSample.load();&nbsp; &nbsp; &nbsp; &nbsp; ControllerSample controllerSample = (ControllerSample) loaderSample.getController();&nbsp; &nbsp; &nbsp; &nbsp; TextArea textAreaSample = controllerSample.getTextAreaSample();&nbsp; &nbsp; &nbsp; &nbsp; textAreaSample.setText("\ndebug textAreaSample\n");&nbsp; &nbsp; &nbsp; &nbsp; TextArea textAreaSendingPhotos = controllerSample.getSendingController().getSendingPhotosController()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .getTextAreaSendingPhotos();&nbsp; &nbsp; &nbsp; &nbsp; textAreaSendingPhotos.setText("test test test");&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setTitle("Hello World");&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setScene(new Scene(root, 800, 400));&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.show();&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; launch(args);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java