栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

尝试运行Java fx应用程序时出现“ FXMLLoader.constructLoadException”

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

尝试运行Java fx应用程序时出现“ FXMLLoader.constructLoadException”

我建议更改项目结构。资源不应驻留在源本身中。所以我的建议是以下项目结构:

src|-main    |-java <-- This should be your 'sources root'        |-application <-- The application itself is within the new sources root        |-controllers |here are my controllers        |-dao        |-service    |-resources <-- all resources (css files, images, fxml files) should be with a subfolder of the resources. This folder should be marked as 'resources root'        |-css        |-images        |-view |here are the fxml files

可以在任何IDE中设置此结构。如何设置它取决于IDE本身。通常,您只需右键单击概述中的文件夹,然后将其标记为源/资源根。“源根”和“资源根”的确切名称取决于IDE。

Main
现在是在包装
application
MainUIController
在包
controllers

在FXML文件中,您应该指定:

fx:controller="controllers.MainUIController"

加载FXML文件应适用于:

AnchorPane root = (AnchorPane) FXMLLoader.load(getClass().getClassLoader().getResource("view/MainUI.fxml"));

至于下面的解释参考,看看这里:

ClassLoader
始终从你的资源的根本。如果仅使用
getClass
,则资源的路径是相对路径。在处理了几次之后,我自己决定始终使用绝对路径,因此使用
getClassLoader

如上面链接中所述,请注意

getClass().getClassLoader().getResource("view/MainUI.fxml");

可以替换为

getClass().getResource("view/MainUI.fxml");

我认为我在某处阅读过,最好直接使用

InputStream
if(如果可用),因为在JAR-
Files中处理URL有时会很棘手(但我目前找不到支持该引用的参考)。因此,可以替代
getResource(..)
使用:

FXMLLoader loader = new FXMLLoader();Parent root = loader.load(getClass().getClassLoader().getResourceAsStream("view/MainUI.fxml");


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/454873.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号