maven - 如何将包控制器添加到使用 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
我遵循以下步骤:
将包控制器添加到
helloworld
包中。在资源包中
添加一个包 fxmlhelloworld
将
App.java
、PrimaryController.java
和SecondaryController.java
放入包控制器中。
将primary.fxml
fxml放入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");
}
- 中
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;
- 中
pom.xml
,替换
<configuration>
<mainClass>com.whatever.fx.helloworld.App</mainClass>
</configuration>
和
<configuration>
<mainClass>com.whatever.fx.helloworld.controller.App</mainClass>
</configuration>
- 中
primary.fxml
,替换
fx:controller="com.whatever.fx.helloworld.PrimaryController">
和
fx:controller="com.whatever.fx.helloworld.controller.PrimaryController">
- 中
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.
解决方案
推荐阅读
- java - IntelliJ java.lang.NoClassDefFoundError: org/apache/log4j/Logger
- javascript - OnChange function of input type radio in reactjs?
- c# - NLog not creating files
- javascript - ajax POST - 响应的 HTTP 状态代码为 403,标头丢失,始终为 OPTIONS
- python - pygsheets set_dataframe:IndexError:列表索引超出范围
- php - 使用 GET 方法逃避 MYSQL 注入
- python - Keras: Masking and Flattening
- java - JFrame的组件在最大化进入全屏模式时不会分别调整大小
- javascript - Node.js vm:如何通过外部事件终止脚本(不使用超时)?
- c# - jQuery Datatables json AJAX 帮助请“表中没有可用数据”