【发布时间】:2019-02-25 10:15:54
【问题描述】:
我想将我的 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()?
【问题讨论】:
-
关键是我没有收到任何错误,没有什么是空的,即使(在检查警报后)
textArea包含附加文本。它只是不在TextArea中显示。 -
如果您有嵌套的 FXML,则不应单独加载它们。内部 FXML 会自动与外部 FXML 一起加载。 textArea 可能存在两次。显示一个,另一个是您在代码中签入的那个。
-
我还以为可以有更多的textAreas。但是这个
textAreaSample放在主FXML 文件中。所以,首先我开始用这个进行测试。 -
Grrr。您加载 sample.fxml 两次。第二次直接在primaryStage.setTitle 之前。这会破坏你之前所做的。