[英]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.