[英]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有两种加载方式,即URL
或InputStream
。
如果要使用File
,请尝试以下操作:
FXMLLoader.load(new FileInputStream(new File("MainFXML.fxml")));
并尝试捕获可能发生的异常。
方法load
具有不同的实现,但是没有一个将String
作为参数。
getClass().getResource("MainFXML.fxml");
返回URL
。 URL
是load
的有效参数。 而已。
综上所述,没有load(String)
。
您可以通过不同的方式检索URL。 在本官方教程中对其进行了描述。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.