gpt4 book ai didi

java - getClass().getResource在加载fxml文件中的作用是什么?

转载 作者:行者123 更新时间:2023-12-01 09:00:53 25 4
gpt4 key购买 nike

FXMLLoader类的

load()方法用于加载FXML文件。那么 getClass().getResource() 是在做什么

Parent root = FXMLLoader.load(getClass().getResource("MainFXML.fxml"));

哪里出了问题

Parent root = FXMLLoader.load(("MainFXML.fxml"));

最佳答案

类(class)。 getResource用于检索可在类路径中找到的资源的URL

FXMLLoader 然后 loads具有此 URL 的文件。

FXMLLoader有两种加载方式,通过 URL 或通过 InputStream

如果您想使用文件,请尝试以下操作:

FXMLLoader.load(new FileInputStream(new File("MainFXML.fxml")));

并 try catch 可能发生的异常。

关于java - getClass().getResource在加载fxml文件中的作用是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41678391/

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