JavaFX SimpleSwingBrowser自动调整网页
为了在我的Swing应用程序中支持OAuth2,我需要加载Google提供的网页URL。由于JavaFX有更好的web支持,我决定使用 由于Oracle提供的代码示例没有“auto-fit网页”功能,因此我将得到类似以下内容的显示 最接近的例子是。但是,该示例要求我们将自定义的JavaFX SimpleSwingBrowser自动调整网页,java,swing,webview,javafx,Java,Swing,Webview,Javafx,为了在我的Swing应用程序中支持OAuth2,我需要加载Google提供的网页URL。由于JavaFX有更好的web支持,我决定使用 由于Oracle提供的代码示例没有“auto-fit网页”功能,因此我将得到类似以下内容的显示 最接近的例子是。但是,该示例要求我们将自定义的标记注入WebView的内容中,这不是我想要做的事情。我更喜欢HTML内容加载,全部由“WebView”完成。这对我很有用: import javafx.application.Application; import j
标记注入WebView
的内容中,这不是我想要做的事情。我更喜欢HTML内容加载,全部由“WebView”完成。这对我很有用:
import javafx.application.Application;
import javafx.concurrent.Worker;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class WebViewHeightTest extends Application {
@Override
public void start(Stage primaryStage) {
String initialURL = "http://stackoverflow.com/questions/23170817/javafx-simpleswingbrowser-auto-fit-web-page" ;
BorderPane root = new BorderPane();
TextField locationBar = new TextField(initialURL);
WebView webView = new WebView();
WebEngine engine = webView.getEngine();
locationBar.setOnAction(event -> engine.load(makeUrl(locationBar.getText())));
root.setTop(locationBar);
root.setCenter(webView);
// Get full width and height of page when it's loaded:
engine.getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> {
if (newState == Worker.State.SUCCEEDED) {
int width = (Integer) engine.executeScript("document.body.scrollWidth");
int height = (Integer) engine.executeScript("document.body.scrollHeight");
System.out.printf("[%d, %d]%n", width, height);
}
});
engine.load(initialURL);
Scene scene = new Scene(root, 600, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
private String makeUrl(String text) {
if (text.contains("://")) {
return text ;
} else {
return "http://"+text ;
}
}
public static void main(String[] args) {
launch(args);
}
}
哇!印象深刻我意识到我仍然需要添加某些偏移量,以使其完全不可滚动
新尺寸(宽度+16,高度+56)
。有什么想法吗?我曾尝试使用隐藏滚动条,但我可以通过突出显示内容并拖动鼠标来测试加载的页面是否可滚动。我不确定我是否完全理解,但如果您将WebView设置为足够大以容纳内容,滚动条将被删除。如果该javascript没有提供正确的维度,请使用中列出的一些其他javascript进行实验
import javafx.application.Application;
import javafx.concurrent.Worker;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class WebViewHeightTest extends Application {
@Override
public void start(Stage primaryStage) {
String initialURL = "http://stackoverflow.com/questions/23170817/javafx-simpleswingbrowser-auto-fit-web-page" ;
BorderPane root = new BorderPane();
TextField locationBar = new TextField(initialURL);
WebView webView = new WebView();
WebEngine engine = webView.getEngine();
locationBar.setOnAction(event -> engine.load(makeUrl(locationBar.getText())));
root.setTop(locationBar);
root.setCenter(webView);
// Get full width and height of page when it's loaded:
engine.getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> {
if (newState == Worker.State.SUCCEEDED) {
int width = (Integer) engine.executeScript("document.body.scrollWidth");
int height = (Integer) engine.executeScript("document.body.scrollHeight");
System.out.printf("[%d, %d]%n", width, height);
}
});
engine.load(initialURL);
Scene scene = new Scene(root, 600, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
private String makeUrl(String text) {
if (text.contains("://")) {
return text ;
} else {
return "http://"+text ;
}
}
public static void main(String[] args) {
launch(args);
}
}