2012-05-02 133 views
3

我想從子文件夾中加載一個fxml,但它失敗。我有一個做替換場景內容的行:JavaFX 2.0加載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) 

任何人都有我需要修理的任何ideea,所以我可以讓這個替換場景內容工作?

感謝

回答

6

如果要定義在FXML文件中像這樣的Button動作:

<Button text="Login" onAction="#processLogin"/> 

,那麼你必須定義在控制器類中的方法類似如下。請注意0​​的簽名:

@FXML 
    private void processLogin(javafx.event.ActionEvent event) { 
     // Process Login 
    } 
+1

我已經有了,但同時我設法通過卸載並重新安裝JavaFX 2.0來解決我的問題!作爲閱讀這些答案的人的提示,請遵循上述建議,如果這不起作用,請嘗試取消隱藏並重新安裝JavaFX 2.0 SDK。 –