【问题标题】:javafx webview load local css filesjavafx webview加载本地css文件
【发布时间】:2014-07-04 04:51:33
【问题描述】:

我有一个WebView,它会加载我保存在项目中的本地 html 文件。我使用以下内容加载文件:

InputStream is = Browser.class.getResourceAsStream(location);
String str = "";
int i;
while((i = is.read()) != -1){
    str += (char)i;
}

str = str.replace("{placeholder_1}", value1);
str = str.replace("{placeholder_2}", value2);


webEngine.loadContent(str);

在 HTML 中,我有一个指向 css 文件的链接。 HTML 文件和 css 文件位于同一目录中,但在 WebView 中加载页面时未加载 css 文件。这是我从 HTML 调用 css 文件的方式:

<link rel="stylesheet" href="main.css" />

为什么文件没有加载?根据其他人的说法,这就是他们的工作方式并且正在发挥作用。为什么它对我不起作用,我做错了什么?

这是目录布局:

【问题讨论】:

    标签: java html css webview javafx


    【解决方案1】:

    将css文件与html文件放在同一目录下,如果

    webView.getEngine().load(location);
    

    被使用。但是它不适用于loadContent()。您需要将 css 文件位置明确定义为:

    (伪代码)

    str = str.replace("href='main.css'", 
                      "href='" + getClass().getResource("main.css") + "'");
    

    【讨论】:

    • I just found out 使用&lt;base&gt; 标签是使用loadContent(String) 加载相关资源的技巧。我为这样的解决方案搜索了很长时间,所以我想分享它。 :)
    【解决方案2】:

    下面的东西对我有用

    项目结构

    Application
    |
     src
        |
        package
               |
               WebViewLoadLocalFile.java
               test.html
               test.css
    

    WebViewLoadLocalFile

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.web.WebView;
    import javafx.stage.Stage;
    
    public class WebViewLoadLocalFile extends Application {
    
        @Override
        public void start(Stage stage) throws Exception {
            BorderPane borderPane = new BorderPane();
            WebView webView = new WebView();
            String url = getClass().getResource("test.html").toExternalForm();
            webView.getEngine().load(url);
            borderPane.setCenter(webView);
            final Scene scene = new Scene(borderPane);
            stage.setScene(scene);
            stage.setHeight(300);
            stage.setWidth(250);
            stage.show();
    
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    test.html

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
    <title>Insert title here</title>
    <link rel="stylesheet" href="test.css" />
    </head>
    <body>
    Test HTML
    </body>
    </html>
    

    test.css

    body
    {
      background-color:#d0e4fe;
    }
    

    【讨论】:

    • 在页面上显示实际 HTML 之前,我在 html 中有占位符。直接加载文件不起作用。我已经更新了问题。
    • 对我来说,当我在eclipse中运行上面的代码时,我可以加载html文件中指定的css。但是,当我将它编译为 jar 文件并运行它时,它不起作用。我认为这与在jar文件中找到css文件的路径的问题有关。
    • @mk7 将资源复制到 jar 取决于项目的配置方式。我认为用必要的数据创建一个新问题会有很大帮助。
    • 太棒了。有效。 String url = getClass().getResource("test.html").toExternalForm(); webView.getEngine().load(url); 这成功了
    猜你喜欢
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    • 2015-01-16
    相关资源
    最近更新 更多