要创建新版本,将提示用户单击GUI左下方的加号按钮,当他们单击该按钮时,我希望在GUI中心顶部放一个窗格,以便用户输入设置并指定选项对于新版本,我已经尝试过:
private void addRelease(Event event) throws IOException {
Popup popup = new Popup();
NewReleasePopupController controller = new NewReleasePopupController();
FXMLLoader loader = new FXMLLoader(getClass().getResource("Resources/NewReleasePopup.fxml"));
loader.setController(controller);
loader.load();
popup.getContent().add((Parent)loader.load());
}
但是,它似乎在加载时会引发错误,我想尽可能避免加载单独的阶段,并且希望将弹出窗口的控制器嵌套在主阶段的控制器内。如果有人可以帮助我完成此工作,或者有任何更好的方法来实现此帮助,我将尝试使用popup类,将不胜感激!
错误:
Caused by: javafx.fxml.LoadException: Controller value already specified.
/D:/DropDayAIO/out/production/DropDayAIO/DropDayAIO/Resources/newReleasePopup.fxml:6
at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2621)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:922)
at javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:980)
at javafx.fxml/javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:227)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:752)
at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
at DropDayAIO.HomeSceneController.addRelease(HomeSceneController.java:151)
at DropDayAIO.HomeSceneController.addNewRelease(HomeSceneController.java:128)
... 57 more
Based on the error you mentioned, I think you already have a controller declaration in the FXML file. like with attribute.. fx:controller
Usually while having this attribute declaration in fxml , if you try to set controller again, it throws Caused by: javafx.fxml.LoadException: Controller value already specified
And I believe this should be the case, because you can notice the error is raised at processAttribute method.
javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:980)
You can either remove it from the fxml or dont need to set in the code (through setController()). If you say, you want the reference of the controller, you can always get through loader.getController()
.
最重要的是,如果您设法解决Controller value already specified
错误,我想接下来您将遇到Caused by: javafx.fxml.LoadException: Root value already specified
。因为您尝试两次调用FXMLLoader的load()方法。将节点分配给变量,然后使用该变量在弹出窗口中进行设置。
我相信以下代码可以解决该问题。试一下!!
private void addRelease(Event event) throws IOException {
Popup popup = new Popup();
FXMLLoader loader = new FXMLLoader(getClass().getResource("Resources/newReleasePopup.fxml"));
Parent parent = (Parent)loader.load(); // I am not sure you need a cast here
newReleasePopupController controller = loader.getController();
popup.getContent().add(parent);
}
建议:请使用适当的类命名约定。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句