我想从我的控制器类处理阶段事件(即隐藏)。所以我所要做的就是添加一个这样的监听器:
((Stage) myPane.getScene().getWindow()).setOn*whatIwant*(...);
但问题是初始化在这段代码之后立即开始:
Parent root = FXMLLoader.load(getClass().getResource("MyGui.fxml"));
在这段代码之前:
Scene scene = new Scene(root);
stage.setScene(scene);
Thus getScene
返回空值。
我自己发现的唯一解决方法是添加一个监听器myPane.sceneProperty
,当它变得不为空时,我得到场景,添加到它的windowProperty
我的听众正在处理我终于检索到的舞台。这一切都以设置所需的侦听器来舞台事件而告终。
我觉得听众太多了。
这是解决我的问题的唯一方法吗?
您可以从以下位置获取控制器的实例FXMLLoader
初始化后通过getController()
,但是你需要实例化一个FXMLLoader
而不是使用静态方法。
打电话后我会通过舞台load()
之后直接到控制器:
FXMLLoader loader = new FXMLLoader(getClass().getResource("MyGui.fxml"));
Parent root = (Parent)loader.load();
MyController controller = (MyController)loader.getController();
controller.setStageAndSetupListeners(stage); // or what you want to do
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)