首页 > 解决方案 > 如何将包控制器添加到使用 Netbeans 12 中的 FXML JavaFX Maven Archetype (Gluon) 创建的项目中?

问题描述

当我在 Netbeans 12 中使用 FXML JavaFX Maven Archetype (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包中。在资源包中
    添加一个包 fxmlhelloworld

  2. App.javaPrimaryController.javaSecondaryController.java放入包控制器中。
    primary.fxmlfxml放入secondary.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.

标签: mavenjavafxgluon

解决方案


推荐阅读