JavaFX 表演舞台

JavaFX show stage

我将 JavaFX 项目从 NetBeans 导入到 Eclipse。奇怪的是,我无法执行在 NetBeans 中运行良好的代码。我用 SceneBuilder 设置了一个小图形用户界面。我只想显示一点 .fxml - 此时根本没有功能代码。我的主要 class 看起来像这样:

public class Main extends Application {

public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.setTitle(Strings.appName);
        stage.getIcons().add(new Image("sql.png")); 
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

但是当我尝试执行代码时,eclipse 编译器 returns:

Exception in Application start method java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source) at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source) Caused by: java.lang.RuntimeException: Exception in Application start method at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication6(Unknown Source) at java.lang.Thread.run(Unknown Source) Caused by: java.lang.NullPointerException: Location is required. at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) at javafx.fxml.FXMLLoader.load(Unknown Source) at core.Main.start(Main.java:27) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication13(Unknown Source) at com.sun.javafx.application.PlatformImpl.lambda$runAndWait6(Unknown Source) at com.sun.javafx.application.PlatformImpl.lambda$null4(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.lambda$runLater5(Unknown Source) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.lambda$null9(Unknown Source) ... 1 more Exception running application core.Main

这是我的项目结构:

SQL
│   .classpath
│   .project
│
├───.settings
│       org.eclipse.jdt.core.prefs
│
├───bin
│   ├───gui
│   │       FXMLDocument.fxml
│   │       FXMLDocumentController.class
│   │       Main.class
│   │       sql.png
│   │
│   ├───print
│   │       Allgemein.class
│   │       Mahnung.class
│   │       PDF.class
│   │       Rechnung.class
│   │
│   └───various
│           Strings.class
│
└───src
    └───gui
            FXMLDocument.fxml
            FXMLDocumentController.java
            Main.java

我在这里做错了什么?或者 eclipse 在这里期待一些东西,而 NetBeans 不是?!?

正如@James_D 所建议的那样,我能够使用

找到问题所在
System.out.println(getClass().getResource("Main.class"));

System.out.println(getClass().getResource("FXMLDocument.fxml"));

在将项目从我的 ubuntu 笔记本电脑 运行 NetBeans 复制到我的主 Windows 机器 运行 10 和 Eclipse 时,我的类路径一定被破坏了。我修复了它,现在一切正常。