如何解决如何在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
我按照以下步骤操作:
-
将软件包控制器添加到
helloworld
软件包中。
在helloworld
资源包中添加包fxml -
将
App.java
,PrimaryController.java
和SecondaryController.java
放入程序包控制器中。
将primary.fxml
和secondary.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");
}
- 在
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.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。