How to set the JavaFX WebView to be as large as the scene?

I have a JavaFX WebView, and I want it to be as big as the scene at the beginning If I could resize the scene, it would be perfect

@Override
public void start(Stage stage) {
    try {
        Scene scene = new Scene(createWebViewPane(),1920,1080);
        scene.getStylesheets().add(getClass().getResource("Styles/application.css").toExternalForm());
        stage.setScene(scene);
        stage.show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

The above code creates a scene with a width of 1920px and a height of 1080px Createwebviewpane() creates a WebView on the pane

private Pane createWebViewPane() {
    final Pane pane = new Pane();
    pane.minWidth(1920);
    pane.minHeight(1080);
    WebView browser = new WebView();
    browser.minWidth(1920);
    browser.prefWidth(1920);
    browser.minHeight(1080);
    browser.prefHeight(1080);
    WebEngine webEngine = browser.getEngine();
    webEngine.load(getClass().getResource("index.html").toExternalForm());
    pane.getChildren().add(browser);
    return pane;
}

There, I try to set the width / height I also set it in the style sheet

web-view{
    -fx-font-scale: 1;  
    -fx-min-width: 1920px;   
    -fx-min-height: 1080px;   
    -fx-pref-width: 100%; 
    -fx-pref-height: 100%;
}

But none of these properties seem to affect WebView It still has the default size 800 pixels wide and 600 pixels high

Solution

Adding WebView to stackpane, "will try to resize each child node to fill its content area." Resize the frame to see how stackpane rearranges WebView content according to the default pos.center

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

/** @see https://stackoverflow.com/a/33824164/230513 */
public class WebViewPane extends Application {

    @Override
    public void start(Stage primaryStage) {
        StackPane root = new StackPane();
        WebView webView = new WebView();
        WebEngine webEngine = webView.getEngine();
        webEngine.load("http://www.example.com");
        root.getChildren().add(webView);
        Scene scene = new Scene(root);
        primaryStage.setTitle("WebView");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>