2017-06-20 51 views
-1

當我把方法「openPlanes」放入按鈕的選項「onAction」。 這顯示了很多錯誤。 但是,如果我刪除「openPlanes」他的開放正常。無法在JavaFX中打開一個新窗口

@FXML 
    private void openPlanes() { 
     openStage("view/Cadastro.fxml"); 
    } 

private void openStage(String fxml) { 
     try { 
      Stage currentStage = (Stage) PLANE.getScene().getWindow(); 
      Parent root = FXMLLoader.load(getClass().getResource(fxml)); 
      Scene scene = new Scene(root); 
      Stage stage = new Stage(StageStyle.TRANSPARENT); 
      stage.setScene(scene); 
      stage.show(); 
      currentStage.hide(); 

     } catch (IOException ex) { 
      Logger.getLogger(mainController.class.getName()).log(Level.SEVERE, null, ex); 
     } 

    } 

enter image description here

引起:javafx.fxml.LoadException:未指定控制器。 文件:/ C:/Users/diego/Documents/NetBeansProjects/Automekanik/DGDSoft/dist/run708547813/DGD%20Soft.jar /dgdsoft/view/MainDGD.fxml:23

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597) 
at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103) 
at javafx.fxml.FXMLLoader$Element.getControllerMethodHandle(FXMLLoader.java:557) 
at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:599) 
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770) 
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104) 
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097) 
at dgdsoft.DGDSoft.start(DGDSoft.java:19) 
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863) 
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) 
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) 
at java.security.AccessController.doPrivileged(Native Method) 
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) 
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) 
... 1 more 

異常運行的應用程序dgdsoft .DGDSoft Java結果:1

+0

有什麼錯誤?什麼是'openStage(...)'? –

+0

我分析這傢伙的應用 GitHub的 - https://github.com/mlayah/bookingFX/tree/master/src/bookingfx YouTube的 - https://www.youtube.com/watch?v=ooT0Ueyngeo 我做了一些平等的部分來學習更多,我試圖修改一些。 現在我試圖調用第二個窗口,但我不明白這個錯誤,並且這個代碼 –

+0

「沒有指定控制器」意味着你沒有在你的FXML文件中指定控制器類。 –

回答

0

首先確保您要加載的fxml資源位於您期望的目錄中。如果它位於相應的目錄中,則打開fxml文件並搜索字符串fx:controller。 確保控制器位於規定的包裝內。

如果你沒有找到字符串FX:控制器在FXML文件,那麼你需要編程做得一樣:

private Scene getScene(String fxmlPath, ControllerClass controller) { 
     FXMLLoader loader; 
     Parent parent; 
     Scene scene; 
     try { 
      //not FXMLLoader.load(getClass().getResource(fxmlPath) 
      loader = new FXMLLoader(getClass().getResource(fxmlPath)); 
      loader.setController(controller); 
      parent = loader.load(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      return null; 
     } 
     scene = new Scene(parent); 

     return scene; 

    } 

最後,提交FXML文件和代碼。