我正试着去掉网页浏览器上的白色背景,我试过了工作和其他东西,但似乎什么都不起作用
public static String url = "URL"; //lets say this has a transparent image on it (it does in my case)
public static Scene FrameWorks;
public static StackPane InnerFrame;
public static WebView viewer;
public static WebEngine webEngine;
public static void web() {
viewer = new WebView();
webEngine = viewer.getEngine();
webEngine.executeScript("document.width");
WebSite(url);
}
public static void WebSite(String URL) {
webEngine.executeScript("window.location = \""+URL+"\";");
}
public void start(Stage Level) {
web();
InnerFrame = new StackPane();
FrameWorks = new Scene(InnerFrame, 909, 609);
InnerFrame.getChildren().add(viewer);
FrameWorks.setFill(Color.TRANSPARENT);
InnerFrame.setStyle("-fx-background-color: rgba(255, 0, 0, 0);");
Level.setScene(FrameWorks);
Level.initStyle(StageStyle.TRANSPARENT);
Level.show();
}发布于 2019-03-14 02:33:23
我不确定我是否完全理解你的问题,如果我错了,请纠正我。您是否希望webview以外的所有内容都是透明的?为了或多或少地复制粘贴另一篇文章的评论,在这里,我(我们)想要一个最小的,完整的,可验证的例子来证明这个问题。在问你自己的问题之前,你还应该记得检查这个问题是否已经被问过和回答过了。
现在我已经解决了这个问题,这里有一个例子,你可以看到堆栈窗格有一个黑色的背景,而webview有一个白色的背景。
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.scene.layout.Background;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
WebView v = new WebView();
v.setOpacity(1); // 0 = fully transparent, 1 = fully visible.
v.setPrefWidth(100);
v.setMaxWidth(100);
v.setMaxHeight(100);
v.setPrefHeight(100);
v.setStyle("-fx-background-color:black;");
StackPane root = new StackPane();
root.getChildren().add(v);
root.setStyle("-fx-background-color:black;");
/*
* Remove the setStyle above, and use either one of these to only have the webview
* visible. root.setBackground(Background.EMPTY);
* root.setStyle("-fx-background-color:TRANSPARENT;");
*/
Scene scene = new Scene(root, 300, 250);
scene.setFill(Color.TRANSPARENT);
primaryStage.setScene(scene);
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.show();
}
}如果删除该行:
root.setStyle("-fx-background-color:black;");并将其替换为当前注释中的任意一行,那么您现在将只能看到webview:
/*
* Remove the setStyle above, and use either one of these to only have the webview
* visible.
* root.setBackground(Background.EMPTY);
* root.setStyle("-fx-background-color:TRANSPARENT;");
*/当然,如果你出于某些原因想要这样做,你也可以在实际的webview上设置透明度:
v.setOpacity(0); /*0.0 Fully Transparent - 1.0 Fully Visible*/请注意,为了做到这一点,我必须将StageStyle设置为透明,不幸的是,我不确定是否可以用其他方式来实现。也许其他人可以在可能或不可能的情况下发表评论。
我希望这能回答你的问题。
https://stackoverflow.com/questions/55147948
复制相似问题