如何在Netbeans 12中将包控制器添加到使用FXML JavaFX Maven ArchetypeGluon创建的项目中?

问题描述

当我在Netbeans 12中使用FXML JavaFX Maven原型(Gluon)创建项目时,我在项目中得到以下文件层次结构:

└── helloworld
    ├── App.java
    ├── PrimaryController.java
    └── SecondaryController.java

没关系,但是我想改成以下结构:

└── helloworld
    └── controller
        ├── App.java
        ├── PrimaryController.java
        └── SecondaryController.java
└── resources
    └── com
        └── whatever
            └── fx
                └── helloworld
                    └── fxml
                        ├── primary.fxml
                        └── secondary.fxml

我按照以下步骤操作:

  1. 将软件包控制器添加到helloworld软件包中。
    helloworld资源包中添加包fxml

  2. App.javaPrimaryController.javaSecondaryController.java放入程序包控制器中。
    primary.fxmlsecondary.fxml放入包fxml

代码Java

public class App extends Application {

    private static Scene scene;

    @Override
    public void start(Stage stage) throws IOException {
        scene = new Scene(loadFXML("fxml/primary"),640,480);
        stage.setScene(scene);
        stage.show();
    }

    public static void setRoot(String fxml) throws IOException {
        scene.setRoot(loadFXML(fxml));
    }

    private static Parent loadFXML(String fxml) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
        return fxmlLoader.load();
    }

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

    @FXML
    private void switchToSecondary() throws IOException 
    {
        App.setRoot("fxml/secondary");
    }
}

public class SecondaryController {

    @FXML
    private void switchToPrimary() throws IOException {
        App.setRoot("fxml/primary");
    }

  1. Module-info.java中,替换
opens com.whatever.fx.helloworld to javafx.fxml;
exports com.whatever.fx.helloworld;

使用

opens com.whatever.fx.helloworld.controller to javafx.fxml;
exports com.whatever.fx.helloworld.controller;
  1. pom.xml中,替换
<configuration>
    <mainClass>com.whatever.fx.helloworld.App</mainClass>
</configuration>

使用

<configuration>
    <mainClass>com.whatever.fx.helloworld.controller.App</mainClass>
</configuration>
  1. primary.fxml中,替换
fx:controller="com.whatever.fx.helloworld.PrimaryController">

使用

fx:controller="com.whatever.fx.helloworld.controller.PrimaryController">
  1. secondary.fxml中,替换
x:controller="com.whatever.fx.helloworld.SecondaryController">

使用

fx:controller="com.whatever.fx.helloworld.controller.SecondaryController">

但是,按照上述所有步骤操作后,出现以下错误:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.IllegalStateException: Location is not set.
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2459)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
    at com.whatever.fx.helloworld/com.whatever.fx.helloworld.controller.App.loadFXML(App.java:31)
    at com.whatever.fx.helloworld/com.whatever.fx.helloworld.controller.App.start(App.java:20)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
    ... 1 more
Exception running application com.whatever.fx.helloworld.controller.App
Command execution failed.

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)