我有两个阶段名为primary和second . 每个阶段都有一个按钮和一个文本字段 . 我想要做的是当我点击阶段主要的按钮,它显示第二阶段,当我在第二阶段点击按钮时,它从第二阶段的文本字段中获取文本,并将文本放在第一阶段的文本字段中 . 请原谅我可怜的英语 . 读完我的代码后你会知道的 .
//PrimaryController
public class PrimaryController {
@FXML
private TextField name;
@FXML
private void handleBtnAction(){
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("second.fxml"));
try {
loader.load();
} catch (IOException e) {
e.printStackTrace();
}
Stage stage = new Stage();
stage.setScene(new Scene(loader.getRoot()));
stage.showAndWait();
}
public void setText(String text){
System.out.println("Setting text");
name.setText(text);
}
}
这是第二个控制器
public class SecondController {
@FXML
private TextField name;
@FXML
private void handleBtn(MouseEvent event){
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("primary.fxml"));
try {
loader.load();
} catch (IOException e) {
e.printStackTrace();
}
PrimaryController controller = loader.getController();
controller.setText(name.getText());
}
}
因此,当我在第二阶段单击按钮时,将在控制台中打印“设置文本”,但不会设置阶段主文本中的文本字段 . 那么,我怎样才能让它发挥作用?