JavaFX提供了一個GUI WebView(javafx.scene.web.WebView)節(jié)點,以將HTML5內容呈現(xiàn)到場景圖形上。
WebView節(jié)點是一個小型瀏覽器。
我們可以使用以下代碼加載網(wǎng)頁并顯示它。
WebView browser = new WebView(); WebEngine webEngine = browser.getEngine(); webEngine.load("http://mySite.com");
完整的源代碼
import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.concurrent.Worker; import javafx.concurrent.Worker.State; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.ScrollPane; import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; import javafx.stage.Stage; public class Main extends Application { @Override public void start(final Stage stage) { stage.setWidth(400); stage.setHeight(500); Scene scene = new Scene(new Group()); final WebView browser = new WebView(); final WebEngine webEngine = browser.getEngine(); ScrollPane scrollPane = new ScrollPane(); scrollPane.setContent(browser); webEngine.getLoadWorker().stateProperty() .addListener(new ChangeListener<State>() { @Override public void changed(ObservableValue ov, State oldState, State newState) { if (newState == Worker.State.SUCCEEDED) { stage.setTitle(webEngine.getLocation()); } } }); webEngine.load("http://m.hgci.cn"); scene.setRoot(scrollPane); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
上面的代碼生成以下結果。
JavaFX Web API也遵循事件驅動的編程模型。
網(wǎng)頁中的以下JavaScript將彈出一個包含瀏覽器窗口消息的警報對話框。
<script> alert("JavaFX is Awesome"); </script>
當在JavaFX WebView節(jié)點中執(zhí)行代碼時,不會彈出本機對話框窗口。但是,OnAlert事件作為javafx.scene.web.WebEvent對象引發(fā)。
我們可以處理這些事件。要設置處理程序,請使用帶有類型為WebEvent的入站參數(shù)的setOnAlert()方法。
browser.getEngine().setOnAlert((WebEvent<String> wEvent) -> { System.out.println("Alert Event - Message: " + wEvent.getData()); });
完整的源代碼
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.ScrollPane; import javafx.scene.web.WebEngine; import javafx.scene.web.WebEvent; import javafx.scene.web.WebView; import javafx.stage.Stage; public class Main extends Application { @Override public void start(final Stage stage) { stage.setWidth(400); stage.setHeight(500); Scene scene = new Scene(new Group()); final WebView browser = new WebView(); final WebEngine webEngine = browser.getEngine(); ScrollPane scrollPane = new ScrollPane(); scrollPane.setContent(browser); browser.getEngine().setOnAlert((WebEvent<String> wEvent) -> { System.out.println("Alert Event - Message: " + wEvent.getData()); }); webEngine.load("http://m.hgci.cn"); scene.setRoot(scrollPane); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
在事件處理程序中,我們可以決定是否顯示一個對話框。
下表顯示了從javafx.scene.web.WebEngine WebEvents和Properties觸發(fā)它們的事件和操作。
SetOn方法 | 方法屬性 | 描述 |
---|---|---|
setOnAlert() | onAlertProperty() | 處理JavaScript警報方法 |
setOnError() | onErrorProperty() | WebEngine錯誤處理程序 |
setOnResized() | onResizedProperty() | JavaScript調整大小處理程序 |
setOnStatusChanged() | onStatusChanged() | JavaScript狀態(tài)處理程序 |
setOnVisibilityChanged() | onVisibilityChangedProperty() | JavaScript窗口可見處理程序 |
setConfirmHandler() | confirmHandlerProperty() | JavaScript確認窗口 |
上面的代碼生成以下結果。
import javafx.application.Application; import javafx.collections.ListChangeListener; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.ScrollPane; import javafx.scene.web.WebEngine; import javafx.scene.web.WebEvent; import javafx.scene.web.WebHistory; import javafx.scene.web.WebHistory.Entry; import javafx.scene.web.WebView; import javafx.stage.Stage; public class Main extends Application { @Override public void start(final Stage stage) { stage.setWidth(400); stage.setHeight(500); Scene scene = new Scene(new Group()); final WebView browser = new WebView(); final WebEngine webEngine = browser.getEngine(); ScrollPane scrollPane = new ScrollPane(); scrollPane.setContent(browser); browser.getEngine().setOnAlert((WebEvent<String> wEvent) -> { System.out.println("Alert Event - Message: " + wEvent.getData()); }); webEngine.load("http://m.hgci.cn"); final WebHistory history = webEngine.getHistory(); history.getEntries().addListener(new ListChangeListener<WebHistory.Entry>() { @Override public void onChanged(Change<? extends Entry> c) { c.next(); for (Entry e : c.getRemoved()) { System.out.println(e.getUrl()); } for (Entry e : c.getAddedSubList()) { System.out.println(e.getUrl()); } } } ); history.go(0); scene.setRoot(scrollPane); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
上面的代碼生成以下結果。
以下代碼顯示如何從WebEngine進行打印。
PrinterJob job = PrinterJob.createPrinterJob(); if (job != null) { webEngine.print(job); job.endJob(); }
完整的源代碼
import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.concurrent.Worker; import javafx.concurrent.Worker.State; import javafx.print.PrinterJob; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.ScrollPane; import javafx.scene.layout.VBox; import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; import javafx.stage.Stage; public class Main extends Application { @Override public void start(final Stage stage) { stage.setWidth(400); stage.setHeight(500); Scene scene = new Scene(new Group()); VBox root = new VBox(); final WebView browser = new WebView(); final WebEngine webEngine = browser.getEngine(); ScrollPane scrollPane = new ScrollPane(); scrollPane.setContent(browser); webEngine.getLoadWorker().stateProperty() .addListener(new ChangeListener<State>() { @Override public void changed(ObservableValue ov, State oldState, State newState) { if (newState == Worker.State.SUCCEEDED) { PrinterJob job = PrinterJob.createPrinterJob(); if (job != null) { webEngine.print(job); job.endJob(); } } } }); webEngine.load("http://m.hgci.cn"); root.getChildren().addAll(scrollPane); scene.setRoot(root); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.ScrollPane; import javafx.scene.layout.VBox; import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage stage) { stage.setTitle("HTML"); stage.setWidth(500); stage.setHeight(500); Scene scene = new Scene(new Group()); VBox root = new VBox(); final WebView browser = new WebView(); final WebEngine webEngine = browser.getEngine(); ScrollPane scrollPane = new ScrollPane(); scrollPane.setContent(browser); webEngine.loadContent("<b>asdf</b>"); root.getChildren().addAll(scrollPane); scene.setRoot(root); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.ScrollPane; import javafx.scene.layout.VBox; import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage stage) { stage.setTitle("HTML"); stage.setWidth(500); stage.setHeight(500); Scene scene = new Scene(new Group()); VBox root = new VBox(); final WebView browser = new WebView(); final WebEngine webEngine = browser.getEngine(); ScrollPane scrollPane = new ScrollPane(); scrollPane.setStyle("-fx-background-color: white"); scrollPane.setContent(browser); webEngine.loadContent("<b>asdf</b>"); root.getChildren().addAll(scrollPane); scene.setRoot(root); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
上面的代碼生成以下結果。
更多建議: