gpt4 book ai didi

java - JavaFX Stage 中的 WebView 大小

转载 作者:搜寻专家 更新时间:2023-11-01 02:39:13 27 4
gpt4 key购买 nike

我想更改 JavaFx 应用程序的 StageWebView 的大小。

我可以更改窗口大小,但浏览器大小不会从 800px 增加,因此 html 页面的显示不适合所有窗口。

看起来像这样(注意右边的浅灰色区域):

enter image description here

这是页面的一些代码:

public class EfadosApp extends Application {
private Scene scene;

@Override public void start(Stage stage) {
scene = new Scene(new Browser(), 1000,500);
stage.setScene(scene);
stage.show();
}
}

class Browser extends Region {
final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();

public Browser() {
webEngine.load("www.oracle.com");
getChildren().add(browser);
}
}

最佳答案

您可以简单地通过扩展来解决这个问题,例如 StackPane而不是 Region:

class Browser extends StackPane {
...
}

不同之处在于 Region 将子项的大小调整为他们喜欢的大小:

By default a Region inherits the layout behavior of its superclass, Parent, which means that it will resize any resizable child nodes to their preferred size, but will not reposition them. If an application needs more specific layout behavior, then it should use one of the Region subclasses: StackPane, HBox, VBox, TilePane, FlowPane, BorderPane, GridPane, or AnchorPane.

StackPane 尝试调整其子项的大小以适应内容区域时:

The stackpane will attempt to resize each child to fill its content area. If the child could not be sized to fill the stackpane (either because it was not resizable or its max size prevented it) then it will be aligned within the area using the alignment property, which defaults to Pos.CENTER.

这当然也意味着,如果您使用 Region 但要么设置 preferred size WebView 到“大东西”,比如

browser.setPrefSize(5000, 5000);

或者你将WebViewheightPropertywidthProperty绑定(bind)到Stage的相应属性,

Browser browser = new Browser();
browser.browser.prefHeightProperty().bind(stage.heightProperty());
browser.browser.prefWidthProperty().bind(stage.widthProperty());
scene = new Scene(browser, 1000, 500);

它也会像您预期的那样工作。

关于java - JavaFX Stage 中的 WebView 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38432698/

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