【发布时间】:2017-06-06 11:43:05
【问题描述】:
当我使用javafx.scene.web.WebView 加载站点/html 时,该站点似乎受到我的场景自定义样式的影响。一个演示问题的最小示例。
Main.java
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
VBox root = new VBox();
primaryStage.setScene(new Scene(root, 900, 900));
String css = Main.class.getResource("/test.css").toExternalForm();
primaryStage.getScene().getStylesheets().add(css);
WebView webView = new WebView();
root.getChildren().add(webView);
webView.getEngine().load("http://google.pl");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
test.css
.text-area,
.text-field {
-fx-background-color: red;
}
最终我希望有一个像webview.getEngine().dontInheritStyles()这样的方法,当然没有,否则我找不到任何方法。试过了:
webView.getStylesheets().clear();
webView.setStyle(null);
webView.getStyleClass().clear();
他们都没有工作。我认为可以使这项工作(尚未尝试过)的一种方法是在不使用相同场景的子窗口中打开 webview,但是我希望将 webview 嵌入到我现有的应用程序视图中所以这个选项是我最后的选择,我宁愿避免它。
【问题讨论】: