尝试将本地页面加载到JavaFX WebEngine中 [英] Trying to load a local page into JavaFX webEngine
问题描述
我的JavaFX应用程序的选项卡上有一个webView组件,我试图将本地存储的HTML页面加载到:
I have a webView component on a tab in my JavaFX application which I am trying to load an locally stored HTML page into:
WebView browser = new WebView();
WebEngine webEngine = browser.getEngine();
webEngine.load("/webView/main.html");
我的html文档(可能不正确)存储在以下位置:
My html document is (possibly incorrectly) stored in the following location:
其中com.cds.gui包含我尝试加载文件的类.如果我打印出 webEngine.getDocument()
,则会得到 null
-即文档没有被加载.
where com.cds.gui contains the class where I am attempting to load the file. If I print out webEngine.getDocument()
I get null
- i.e. the document isn't getting loaded.
请让我知道我要去哪里了!谢谢.
Please let me know where I'm going wrong! Thanks.
推荐答案
您需要以URL的形式读取本地文件,以便WebEngine可以找到它.例如,您可以使用
You need to read the local file in as a URL so that the WebEngine can find it. For instance, you can find the file as a resouce using
URL url = this.getClass().getResource("/com/cds/gui/webView/main.html");
webEngine.load(url.toString());
或者您可以将实际的String路径加载到File对象中,并使用它来获取String URL.
Or you can load the actual String path into a File object and use it to get the String URL.
File f = new File("full\\path\\to\\webView\\main.html");
webEngine.load(f.toURI().toString());
希望这会有所帮助!
这篇关于尝试将本地页面加载到JavaFX WebEngine中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!