簡體   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