繁体   English   中英

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

[英]What is the role of getClass().getResource in loading fxml file?

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

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

出什么问题了

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

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

然后, FXMLLoader使用此URL 加载文件。

FXMLLoader有两种加载方式,即URLInputStream

如果要使用File ,请尝试以下操作:

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

并尝试捕获可能发生的异常。

方法load具有不同的实现,但是没有一个将String作为参数。

getClass().getResource("MainFXML.fxml"); 返回URL URLload的有效参数。 而已。

综上所述,没有load(String)

您可以通过不同的方式检索URL。 官方教程中对其进行了描述。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM