【问题标题】:passing variables between scenes using JavaFx [duplicate]使用JavaFx在场景之间传递变量[重复]
【发布时间】:2020-02-24 06:25:35
【问题描述】:

我尝试了几乎所有可用的解决方案,但似乎没有任何效果我需要在 2 个 Javafx 场景之间传递 string,但是当我单击导航到下一个场景时,什么也没有发生。到目前为止,这是我的代码:

String myVariable = "test";
FXMLLoader loader =new FXMLLoader(getClass().getResource("/fxml/Comments.fxml"));
Parent root = null;
CommentsController commentsController = new CommentsController();
commentsController = loader.getController();
commentsController.transferMessage(myVariable);
 Stage stage = new Stage();
 stage.setScene(new Scene(root));
 stage.show(); 

PS:我在第二个场景中设置了transferMessage() 方法。

【问题讨论】:

  • 应该有一个NullPointerException 发生。加载程序的controller 属性以及root 保持null,因为您从不调用load() 或为root 分配不同的值。

标签: java javafx


【解决方案1】:
  1. try{}...catch(IOException ex){}包围你的代码块
  2. Parent root = null 替换为Parent root = load.loader()

仅此而已

【讨论】:

  • 最好也应该删除对commentsController变量的初始分配,以避免创建未使用的控制器实例...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 2020-03-23
  • 1970-01-01
相关资源
最近更新 更多