2017-01-16 134 views
0

load()方法FXMLLoader类用于加载FXML文件。那么什么是getClass().getResource()getClass()。getResource在加载fxml文件中的作用是什么?

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

做有什么不对load有不同的实现

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

从类路径中读取文件。 'FXMLLoader.load((“MainFXML.fxml”));'从文件系统加载文件 – Jens

回答

0

方法,但它们都不需要String作为参数。

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

因此,总结一下,没有执行load(String)

您可以通过不同的方式检索URL。他们在this官方教程中描述。

1

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

FXMLLoader然后loads带此URL的文件。

FXMLLoader有两种加载方式,由URLInputStream

如果你想使用File,试试这个:

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

并尝试捕获可能发生的可能的例外。

+0

有更多的方法,而不仅仅是两个:) – xenteros

+0

@xenteros:从上面的链接文档,我没有看到'load'方法采取任何其他比'URL'或'InputStream'更容易获得资源,你有更多的信息吗? – Berger

+0

http://imgur.com/a/3aA0Y – xenteros

相关问题