在JavaFX';s网络视图
我正在全屏使用在JavaFX';s网络视图,java,javascript,css,webkit,javafx,Java,Javascript,Css,Webkit,Javafx,我正在全屏使用WebView来显示固定大小为1024x768的内容,因此我需要对其进行缩放,使其从屏幕的顶部延伸到底部。为了实现这一点,我将以下代码放入控制器的initialize(): 在HTML中,body标记肯定有一个id: <body id="pbody"> SlideshowController.java:165作为行: maincontainer.setAttribute("style", String.format("-webkit-transform: scale(
WebView
来显示固定大小为1024x768
的内容,因此我需要对其进行缩放,使其从屏幕的顶部延伸到底部。为了实现这一点,我将以下代码放入控制器的initialize()
:
在HTML中,body标记肯定有一个id:
<body id="pbody">
SlideshowController.java:165
作为行:
maincontainer.setAttribute("style", String.format("-webkit-transform: scale(%.5f)", (primaryScreenBounds.getHeight() / 768.0)));
我无法理解这种行为背后的原因
更新:我尝试等待加载文档,但也不起作用,首先我手动将其放入内容:
<script>
function scaleBody(scale)
{
alert(3);
document.getElementById('pbody').style.webkitTransform = "scale("+scale+")";
}
</script>
只有警报(1)
实际触发,并且不会生成错误
…仍然不知道如何在JavaFX中执行js。您需要提供更多详细信息,例如您试图加载哪种html内容。以下是我的测试代码,它可以正常工作:
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker.State;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import javafx.stage.Screen;
import javafx.stage.Stage;
import org.w3c.dom.Element;
public class WVTest extends Application {
private WebView wv;
@Override
public void start(Stage primaryStage) {
wv = new WebView();
wv.getEngine().load(this.getClass().getResource("index.html").toExternalForm());
// wv.getEngine().load("http://www.oracle.com/products/index.html");
wv.getEngine().getLoadWorker().stateProperty().addListener(
new ChangeListener<State>() {
@Override
public void changed(ObservableValue<? extends State> ov, State oldState, State newState) {
if (newState == State.SUCCEEDED) {
wv.requestFocus();
wv.getEngine().setUserStyleSheetLocation(getClass().getResource("style.css").toExternalForm());
Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
Element maincontainer = (Element) wv.getEngine().executeScript("document.getElementById('pbody')");
System.out.println("maincontainer = " + maincontainer);
maincontainer.setAttribute("style", String.format("-webkit-transform: scale(%.5f)", (primaryScreenBounds.getHeight() / 768.0)));
}
}
});
VBox root = new VBox();
root.getChildren().addAll(wv);
Scene scene = new Scene(root, 300, 250);
scene.getStylesheets().add(this.getClass().getResource("style.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
和index.html
印刷品
maincontainer=[object HTMLBodyElement]
或者,在常规web浏览器中打开加载的html内容,然后按F12(firebug或类似工具),然后在JS控制台上键入
document.getElementById('pbody')
为什么不执行以缩放值为参数的JS函数?老实说,我不知道有这样的函数,它是标准JS one还是特定于Webkit?两者都有。webkit引擎可以处理的任何JS代码。我的意思是,尝试在一个标记块中编写一个JS函数,该标记块执行document.getElementById()等操作。然后在wv.getEngine().executeScript()中调用这个函数。我可能应该说我对内容没有太多控制权,它是通过外部服务获取的。我不是JavaFX专家。但是,这可能是因为你试图在尸体还没有装好的时候接近尸体。首先,确保在执行initialize()时主体已完成加载。我怀疑是这样。
maincontainer.setAttribute("style", String.format("-webkit-transform: scale(%.5f)", (primaryScreenBounds.getHeight() / 768.0)));
<script>
function scaleBody(scale)
{
alert(3);
document.getElementById('pbody').style.webkitTransform = "scale("+scale+")";
}
</script>
double scale = Screen.getPrimary().getVisualBounds().getHeight() / 768.0;
wv.getEngine().executeScript(String.format("alert(1); window.onload = function(){ alert(2); scaleBody(%.5f); }", scale));
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker.State;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import javafx.stage.Screen;
import javafx.stage.Stage;
import org.w3c.dom.Element;
public class WVTest extends Application {
private WebView wv;
@Override
public void start(Stage primaryStage) {
wv = new WebView();
wv.getEngine().load(this.getClass().getResource("index.html").toExternalForm());
// wv.getEngine().load("http://www.oracle.com/products/index.html");
wv.getEngine().getLoadWorker().stateProperty().addListener(
new ChangeListener<State>() {
@Override
public void changed(ObservableValue<? extends State> ov, State oldState, State newState) {
if (newState == State.SUCCEEDED) {
wv.requestFocus();
wv.getEngine().setUserStyleSheetLocation(getClass().getResource("style.css").toExternalForm());
Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
Element maincontainer = (Element) wv.getEngine().executeScript("document.getElementById('pbody')");
System.out.println("maincontainer = " + maincontainer);
maincontainer.setAttribute("style", String.format("-webkit-transform: scale(%.5f)", (primaryScreenBounds.getHeight() / 768.0)));
}
}
});
VBox root = new VBox();
root.getChildren().addAll(wv);
Scene scene = new Scene(root, 300, 250);
scene.getStylesheets().add(this.getClass().getResource("style.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
body {
overflow-x: hidden;
background-color: #ccc;
}
#pbody {
margin: 0 auto;
-webkit-transform-origin: top center;
}
<html>
<head>
<title>Test</title>
</head>
<body id="pbody">
Test<br/>
Test<br/>
Test<br/>
Test<br/>
</body>
</html>
System.out.println("maincontainer = " + maincontainer);