gpt4 book ai didi

css - 将 CSS 文件应用于 JavaFX WebView

转载 作者:技术小花猫 更新时间:2023-10-29 10:39:23 24 4
gpt4 key购买 nike

我正在尝试将 CSS 文件应用于 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;
}

最佳答案

James_D already explained in his answer如何将“JavaFx CSS”转换为“HTML CSS”,但使用起来可能更方便WebEngine.setUserStylesheetLocation设置包含 CSS 的样式表:

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

style.css 包含 css 代码:

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

关于css - 将 CSS 文件应用于 JavaFX WebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32783532/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com