将CSS文件应用于JavaFX WebView

时间:2015-09-25 13:39:23

标签: css webview javafx

我正在尝试将CS​​S文件应用于JavaFX WebView对象。根据我的阅读,这应该可以解决问题,但显然我错过了一些东西,因为WebView显示没有任何样式。

package net.snortum.play;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class WebviewCssPlay extends Application {

    @Override
    public void start(Stage stage) {
        stage.setTitle("CSS Styling Test");
        stage.setWidth(300);
        stage.setHeight(200);

        WebView browser = new WebView();
        WebEngine webEngine = browser.getEngine();
        webEngine.loadContent("<html><body><h1>Hello!</h1>This is a <b>test</b></body></html>");

        VBox root = new VBox(); 
        root.getChildren().addAll(browser);
        root.getStyleClass().add("browser");
        Scene scene = new Scene(root);
        stage.setScene(scene);
        scene.getStylesheets().add("/net/snortum/play/web_view.css");
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

我的CSS文件如下所示:

.browser {
    -fx-background-color: #00ff80;
    -fx-font-family: Arial, Helvetica, san-serif;
}

2 个答案:

答案 0 :(得分:23)

James_D already explained in his answer如何转换&#34; JavaFx CSS&#34;到&#34; HTML CSS&#34;,但使用WebEngine.setUserStylesheetLocation设置包含CSS的样式表可能更方便:

webEngine.setUserStyleSheetLocation(getClass().getResource("style.css").toString());

style.css 包含css代码:

body {
    background-color: #00ff80; 
    font-family: Arial, Helvetica, sans-serif;
}

答案 1 :(得分:10)

您的代码将CSS应用于JavaFX WebView节点;您正在尝试将CS​​S应用于WebView内显示的HTML文档。由于Web视图没有包含任何文本的JavaFX节点,-fx-font-family无效,并且HTML页面的背景将模糊WebView的背景,因此-fx-background-color将不可见。

为了做你想做的事,你需要操纵加载文档的DOM并应用(标准的,HTML适用的)CSS。这看起来像是:

import javafx.application.Application;
import javafx.concurrent.Worker.State;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;


public class WebViewCssPlay extends Application {

    private static final String CSS = 
              "body {"
            + "    background-color: #00ff80; "
            + "    font-family: Arial, Helvetica, san-serif;"
            + "}";

    @Override
    public void start(Stage stage) {
        stage.setTitle("CSS Styling Test");
        stage.setWidth(300);
        stage.setHeight(200);

        WebView browser = new WebView();
        WebEngine webEngine = browser.getEngine();

        webEngine.getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> {
            if (newState == State.SUCCEEDED) {
                Document doc = webEngine.getDocument() ;
                Element styleNode = doc.createElement("style");
                Text styleContent = doc.createTextNode(CSS);
                styleNode.appendChild(styleContent);
                doc.getDocumentElement().getElementsByTagName("head").item(0).appendChild(styleNode);

                System.out.println(webEngine.executeScript("document.documentElement.innerHTML"));
            }
        });
        webEngine.loadContent("<html><body><h1>Hello!</h1>This is a <b>test</b></body></html>");

        VBox root = new VBox(); 
        root.getChildren().addAll(browser);
        root.getStyleClass().add("browser");
        Scene scene = new Scene(root);
        stage.setScene(scene);
//        scene.getStylesheets().add("/net/snortum/play/web_view.css");
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

编辑:另请参阅@ fabian的答案,这在绝大多数用例中更为清晰,更可取。