我正在尝试从子文件夹加载fxml,但它失败了。我有替换场景内容的行:
private Parent replaceSceneContent(String fxml) throws Exception {
Parent page = (Parent) FXMLLoader.load(App.class.getResource("skinFolder/fxml/"+fxml), null, new JavaFXBuilderFactory());
Scene scene = stage.getScene();
if (scene == null) {
scene = new Scene(page, 700, 450);
scene.getStylesheets().add(App.class.getResource("skinFolder/css/defaultSkin.css").toExternalForm());
stage.setScene(scene);
} else {
stage.getScene().setRoot(page);
}
stage.sizeToScene();
return page;
}
我在下一个方法中使用此函数:
private void gotoLogin() {
try {
replaceSceneContent("login.fxml");
} catch (Exception ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
}
}
问题是我遇到了下一个错误:
javafx.fxml.LoadException: Method processLogin() does not have the correct signature for an event handler.
at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(Unknown Source)
at javafx.fxml.FXMLLoader.processEndElement(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at Main.App.replaceSceneContent(App.java:115)
at Main.App.gotoLogin(App.java:108)
at Main.App.start(App.java:72)
at com.sun.javafx.application.LauncherImpl$5.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$4.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$3.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source)
at com.sun.glass.ui.win.WinApplication$2$1.run(Unknown Source)
at java.lang.Thread.run(Thread.java:722)
任何人都有任何想法我需要修复的东西,所以我可以让这个替换场景内容起作用吗?
由于
答案 0 :(得分:6)
如果您在FXML文件中定义Button
的操作,请执行以下操作:
<Button text="Login" onAction="#processLogin"/>
然后你必须在控制器类中定义一个方法,如下所示。请注意processLogin
的签名:
@FXML
private void processLogin(javafx.event.ActionEvent event) {
// Process Login
}