2017-06-20 49 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文件和代码。