如何将JavaFX WebView设置为与场景一样大? [英] How can I set a JavaFX WebView as big as the scene?

查看:727
本文介绍了如何将JavaFX WebView设置为与场景一样大?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JavaFX WebView,我希望它与开始时的场景一样大。如果我可以通过场景调整大小,这将是完美的。

但是现在我只关注初始大小。

  @Override 
public void start(舞台舞台){
try {
场景场景=新场景(createWebViewPane(),1920,1080);
scene.getStylesheets()。add(getClass()。getResource(Styles / application.css)。toExternalForm());
stage.setScene(场景);
stage.show();
} catch(例外e){
e.printStackTrace();
}
}

以上代码创建一个宽度为1920px的场景,身高:1080px。 createWebViewPane()在窗格上创建WebView。

  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);
返回窗格;
}

我尝试设置宽度/高度。我也在样式表中设置它。

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

但这些属性似乎都不会影响WebView。它仍然具有默认大小。宽度为800像素,高度为600像素。

解决方案

WebView 添加到

  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 http://stackoverflow.com/a/33824164/230513 * /
公共类WebViewPane扩展应用程序{

@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);
场景场景=新场景(根);
primaryStage.setTitle(WebView);
primaryStage.setScene(scene);
primaryStage.show();
}

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

}


I have a JavaFX WebView which I want to have as big as the scene on start. If I could make it resize with the scene, this would be perfect.
But right now I focus just on the initial size.

@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 width: 1920px, height: 1080px. createWebViewPane() creates a WebView on a 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 stylesheet.

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 the WebView. It still has it's default size. 800px in width and 600px in height.

解决方案

Add the WebView to a StackPane, which "will attempt to resize each child to fill its content area." Resize the frame to see how the StackPane reflows the WebView content based on 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 http://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);
    }

}

这篇关于如何将JavaFX WebView设置为与场景一样大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆