我在获取 WebView 内容的大小时遇到问题。这是我的代码
String heightText = (String) engine.executeScript("window.getComputedStyle(document.body,null).getPropertyValue('height')");
double height = Double.valueOf(heightText.replace("px", ""));
String widthText = web1.getEngine().executeScript(
"window.getComputedStyle(document.body,null).getPropertyValue('width')"
).toString();
double width = Double.valueOf(widthText.replace("px", ""));
web1.setPrefHeight(height);
web1.setPrefWidth(width);
System.out.println(height + " " + width);
}
但它第一次引用 0,其他时候它的结果是错误的。
实际上我必须编辑我的问题。上面的代码显示了之前内容的大小..为什么会这样? 请帮忙。谢谢
最佳答案
确保在 Stage.show()
或显示相应窗口的代码之后调用此代码。在 UI 初始化之前,所有高度和宽度 getter 都返回零。
关于java - 将内容适配到 JavaFx 中的 WebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31391517/