透明背景WebEngine javafx

时间:2019-03-13 17:28:50

标签: java javafx

我正在尝试丢失Webviewer上的白色背景 我已经尝试过其他方法,但似乎没有任何作用

    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();
}

1 个答案:

答案 0 :(得分:0)

我不确定我是否完全理解您的问题,如果我错了,请纠正我。您是否希望除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;");
 */

当然,如果您出于某些原因要这样做,当然也可以在实际的Web视图上设置透明度:

v.setOpacity(0); /*0.0 Fully Transparent - 1.0 Fully Visible*/

请注意,要执行此操作,我必须将StageStyle设置为透明,但不幸的是,我不确定是否可以通过其他任何方式进行操作。也许其他人可以发表评论。

我希望这能回答您的问题。

相关问题