gpt4 book ai didi

JavaFX 项目在导出到可运行的 .jar 时中断? FXMLLoader 相关?

转载 作者:行者123 更新时间:2023-11-30 08:37:13 27 4
gpt4 key购买 nike

我一直在尝试将 JavaFX 项目导出为 .jar,但每当我运行它时,我都会收到 NullPointerException;据推测,它找不到 FXML。我不确定如何处理这个问题,我的项目结构有问题吗?我看了看:FXML layout not loaded when run jar outside Eclipse ,但该解决方案似乎对我不起作用,并且有一个不同的问题。

项目托管于:https://github.com/Sunquyman/GameOfLife/tree/master/src

我在尝试运行时收到的堆栈跟踪:

java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at main.Main.start(Main.java:21)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$49(GtkApplication.java:139)
at java.lang.Thread.run(Thread.java:745)

真的很困惑为什么会这样;如果它在 Eclipse 中运行良好,为什么它会在导出时中断?

提前致谢!

最佳答案

首先你必须像这样创建一个资源文件夹:

要执行此操作,请右键单击“项目”->“新建”->“资源文件夹”->根据需要命名,但一个不错的选择是资源

enter image description here

然后我修改了你的代码如下:

主类:

public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {

//You have to Load the font before using it into css!!
Font.loadFont(getClass().getResourceAsStream("/resources/fonts/8bit.ttf"), 14);
Scene scene = new Scene(new GUIController());
scene.getStylesheets().add(getClass().getResource("/view/application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("Game of Life Settings");
primaryStage.show();

} catch (Exception e) {
e.printStackTrace();
}
}

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

GUIController: 请记住,在 .fxml 文件中我使用了 root 和 !default Controller,所以它现在看起来像这样(使用了 SceneBuilder )。基本上这样做你可以使用 GUIController 类超过一次。

enter image description here

public class GUIController extends AnchorPane implements Initializable {

..........

private final String lotOn = "/music/soundfx/lensoftruth_on.mp3";
private final String lotOff = "/music/soundfx/lensoftruth_off.mp3";

.......

//Constructor
public GUIController(){
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/GUI.fxml"));
loader.setRoot(this);
loader.setController(this);

try {
loader.load();
} catch (IOException e) {
e.printStackTrace();
}
}


.......

}

关于 CSS:最后我修改了 css 文件,这导致 css 字体在添加 css(check this here)之前加载到代码中:

@font-face {

-fx-font-family: "8bit";

}

最后关于/:

如果您为类调用 getResource() 并且没有在前面加上/,则该路径被认为是相对于该类的包的。 Check this here

Are you happy? :)

关于JavaFX 项目在导出到可运行的 .jar 时中断? FXMLLoader 相关?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37380297/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com