gpt4 book ai didi

JavaFX FXMLLoader 无法找到文件

转载 作者:行者123 更新时间:2023-12-02 01:07:47 25 4
gpt4 key购买 nike

这是我的第一个问题,所以如果我出错了,请耐心等待,但我对 FXMLLoader 有一个奇怪的问题,我无法解释自己,所以任何提示或帮助将不胜感激。

以下代码片段是我如何将文件 GraphicSettings.fxml 加载到我的程序中。

@FXML
public void showGraphicSettings()
{
System.out.println("Showing graphic settings");
if (graphicSettings == null)
{
try
{
FXMLLoader loader = new FXMLLoader();
System.err.println(getClass().getResource("GraphicSettings.fxml"));
System.err.println(getClass().getResource("/GraphicSettings.fxml"));
loader.setLocation(getClass().getResource("GraphicSettings.fxml"));
System.err.println(loader.getLocation());
graphicSettings = (GridPane) loader.load();

GraphicSettingsController graphicSettingsController = loader.getController();
graphicSettingsController.setSettingsModel(settingsModel);
graphicSettingsController.setMainPresenter(mainPresenter);
System.err.println(graphicSettings);
}
catch (IOException e)
{
System.err.println("GraphicSettings.fxml not found");
e.printStackTrace();
}
}
mainPresenter.setViewContent(graphicSettings);
}

此代码段的输出如下:

file:/D:/Gits/TamClipnyera/bin/settings/GraphicSettings.fxml
null
file:/D:/Gits/TamClipnyera/bin/settings/GraphicSettings.fxml
GraphicSettings.fxml not found
javafx.fxml.LoadException: /D:/Gits/TamClipnyera/bin/settings/GraphicSettings.fxml:9

at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
at clip/settings.MainSettingsController.showGraphicSettings(MainSettingsController.java:81)
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:566)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1784)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3564)
at javafx.graphics/javafx.scene.Scene$ClickGenerator.access$8200(Scene.java:3492)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3860)
at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1200(Scene.java:3579)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)

据我了解,该文件在正确的位置找到,并且它提供给 FXMLLoader 的 URL 也正确,但由于某种原因,FXMLLoader 找不到该文件。

现在,这本身不会那么令人困惑,但我正在运行另外两个 FXMLLoader,其其他子菜单 Pane 的结构相同(一次只显示一个),但它们工作得很好,没有任何问题。

所以,本质上,我在同一个文件夹中有三个 fxml 文件,以相同的方式访问,但我得到了不同的结果,这让我对这种行为的潜在解释感到困惑。

对于任何对我引用的完整代码感兴趣的人:This is the Controller class in question

最佳答案

你的项目结构一团糟,我首先要清理它。您不应将类和 fxml 存储在同一文件夹中。它应该或多或少看起来像这样:

src
|-main
|-java <-- this is for your classes
|-application
|-controllers

|-resources <-- this is there your fxmls should go
|-fxml
|-style

然后尝试以下操作:

GridPane root = (AnchorPane) FXMLLoader.load(getClass().getClassLoader().getResource("fxml/GraphicSetting.fxml"))

确保在您的 fxml 中为 Controller 提供限定名称

关于JavaFX FXMLLoader 无法找到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59800353/

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