2

我在编辑器窗格中加载 HTML 文件并显示它时遇到问题。我正在使用的代码是:

window_pane = new JEditorPane("file:///assets/www/index.html");

但这只是给出了一些错误:

Exception in thread "main" java.io.FileNotFoundException: \assets\www\index.html (Het systeem kan het opgegeven pad niet vinden)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown Source)
    at javax.swing.JEditorPane.getStream(Unknown Source)
    at javax.swing.JEditorPane.setPage(Unknown Source)
    at javax.swing.JEditorPane.setPage(Unknown Source)
    at javax.swing.JEditorPane.<init>(Unknown Source)
    at nl.xedus.battlex.java.WebBrowser.<init>(WebBrowser.java:33)
    at nl.xedus.battlex.java.WebBrowser.main(WebBrowser.java:72)

截屏:

在此处输入图像描述

有人可以帮忙吗?

4

1 回答 1

4

这看起来像是文件 URL 中的相对路径。您需要使用绝对路径。对于与您的应用程序捆绑的资源,您可以获得如下 URL:

final String resourcePath = "foobar.html";
URL resourceURL = Thread.currentThread().getContextClassLoader().getResource(resourcePath);
JEditorPane editorPane = new JEditorPane(resourceURL);

这假定在您的类路径的根目录中有一个名为“foobar.html”的 HTML 文件。扩展伪代码以满足您的需求。

于 2012-02-21T16:36:47.043 回答