Java - 从资产加载html页面

时间:2013-06-02 12:07:30

标签: java

我正在尝试加载位于我的资源文件夹中的html页面。

我可以使用此代码加载在线html网页(例如:http://google.com

    ...
    dtrpnHey = new JEditorPane();
    dtrpnHey.setEditable(false);
    try {
          dtrpnHey.setPage("http://google.com");
        }catch (IOException e) {
            dtrpnHey.setContentType("text/html");
            dtrpnHey.setText("<html>Could not load</html>");
        } 

        JScrollPane scrollPane = new JScrollPane(dtrpnHey);     
        JFrame f = new JFrame("Test HTML");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(scrollPane);
        f.setPreferredSize(new Dimension(800,600));
        f.setVisible(true);
    panel_1.add(dtrpnHey);
            ...

我知道file:///android_asset/mynicehtmlfile.html适用于android,但由于它包含android_assets,我认为它不适用于非Android。

非常感谢!

编辑:假设我的jar文件(在我的项目文件夹中)中有一个文件夹,名为:assets(http://s24.postimg.org/p02ju9xip/Knipsel.jpg?noCache=1370175365

1 个答案:

答案 0 :(得分:1)

editorPane.setPage(SomeClass.class.getResource("/assets/thePage.html"));
如果assets文件夹确实在jar中,

应该这样做。从您发布的图像来看,它不是,因为资产不在您项目的任何源文件夹中。 Eclipse的运行时类路径由项目构建路径中的所有jar组成,并从目标目录组成,它放置已编译的类和源目录中的所有资源。如果未将assets文件夹放在源目录中,Eclipse将不会使其成为运行时类路径的一部分。