原文链接:http://www.javafxchina.net/blog/2015/07/html_popup/
可以使用WebView组件创建的浏览器中的弹出式窗口
当你需要在你的程序中打开一个新的浏览器窗口,
PopupFeatures类的实例会被传递到使用setCreatePopupHandler方法注册到WebEngine对象上的弹出窗口处理器中
使用弹出式窗口设置
你可以为文档设置一个可选的WebView对象,该对象将会在一个单独的窗口中打开。图展示了一个用户可通过右击任何链接来打开的上下文菜单
要想为目标文档指定一个新的浏览器窗口,请参考例中修改过的代码来使用PopupFeatures实例类实现。
需注意:上下文菜单默认对所有的WebView对象都是可用的。如果要为某个特定的WebView实例禁用上下文菜单,可调用setContextMenuEnabled方法,并传入false值:browser.setContextMenuEnabled(false);
help.html 的代码
<html lang="en"> <head> <!-- Visibility toggle script --> <script type="text/javascript"> <!-- function toggle_visibility(id) { var e = document.getElementById(id); if (e.style.display == 'block') e.style.display = 'none'; else e.style.display = 'block'; } //--> </script> </head> <body> <h1>Online Help</h1> <p class="boxtitle"><a href="#" οnclick="toggle_visibility('help_topics');" class="boxtitle">[+] Show/Hide Help Topics</a></p> <ul id="help_topics" style='display:none;'> <li>Products - Extensive overview of Oracle hardware and software products, and summary Oracle consulting, support, and educational services. </li> <li>Blogs - Oracle blogging community (use the Hide All and Show All buttons to collapse and expand the list of topics).</li> <li>Documentation - Landing page to start learning Java. The page contains links to the Java tutorials, developer guides, and API documentation.</li> <li>Partners - Oracle partner solutions and programs. Popular resources and membership opportunities.</li> </ul> </body> </html>
________________
import javafx.application.Platform; /** * @author zhaoyong * @Date 2022/10/20 * @Description */ public class JavaApp { public void exit() { Platform.exit(); } }
________________
import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.HPos; import javafx.geometry.Pos; import javafx.geometry.VPos; import javafx.scene.Node; import javafx.scene.control.Button; import javafx.scene.control.Hyperlink; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.HBox; import javafx.scene.layout.Priority; import javafx.scene.layout.Region; import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; import javafx.concurrent.Worker.State; import netscape.javascript.JSObject; import javafx.scene.web.PopupFeatures; class Browser extends Region { private HBox toolBar; final private static String[] imageFiles = new String[]{//图片路径数组 "folder_16.jpg", "folder_16.jpg", "root.jpg", "root.jpg", "root.jpg" }; final private static String[] captions = new String[]{//标题名称数组 "百度", "Blogs", "Documentation", "Partners", "Help" }; final private static String[] urls = new String[]{//添加的测试url集合 "http://www.baidu.com", "http://blogs.oracle.com/", "http://docs.oracle.com/javase/index.html", "http://www.oracle.com/partners/index.html", Browser.class.getResource("help.html").toExternalForm() }; final ImageView selectedImage = new ImageView();//实例化图标视图对象 final Hyperlink[] hpls = new Hyperlink[captions.length];//定义一个新的超级链接对象 final Image[] images = new Image[imageFiles.length];//创建一个图片数组 final WebView browser = new WebView();//定义一个浏览器内核对象 final WebEngine webEngine = browser.getEngine();//获取浏览器引擎 final Button toggleHelpTopics = new Button("Toggle Help Topics"); private boolean needDocumentationButton = false; final WebView smallView = new WebView(); public Browser() { //apply the styles getStyleClass().add("browser");//外观添加样式类 for (int i = 0; i < captions.length; i++) { final Hyperlink hpl = hpls[i] = new Hyperlink(captions[i]);//创建带标题的超级链接对象 Image image = images[i] = new Image(getClass().getResourceAsStream(imageFiles[i]));//根据文件路径创建相关图片对象 hpl.setGraphic(new ImageView(image));//超级链接上添加图片 final String url = urls[i];//具体url的读取 final boolean addButton = (hpl.getText().equals("Help")); hpl.setOnAction((ActionEvent e) -> {//超级链接对象添加点击处理事件处理回调函数机制的编写 needDocumentationButton = addButton; webEngine.load(url);//让浏览器内核加载url指向的资源 }); } // create the toolbar toolBar = new HBox(); toolBar.setAlignment(Pos.CENTER); toolBar.getStyleClass().add("browser-toolbar");//水平布局盒子对象的外部样式引入 toolBar.getChildren().addAll(hpls);//水平布局盒子上面添加超级链接对象 toolBar.getChildren().add(createSpacer());//创建一个空白区域 //set action for the button toggleHelpTopics.setOnAction((ActionEvent t) -> {//让浏览器内核执行js代码,本例中是调用javascript函数 webEngine.executeScript("toggle_visibility('help_topics')"); }); //add components smallView.setPrefSize(300, 200); //handle popup windows webEngine.setCreatePopupHandler( (PopupFeatures config) -> { smallView.setFontScale(0.8); if (!toolBar.getChildren().contains(smallView)) { toolBar.getChildren().add(smallView); } return smallView.getEngine(); }); // process page loading webEngine.getLoadWorker().stateProperty().addListener( (ObservableValue<? extends State> ov, State oldState, State newState) -> { toolBar.getChildren().remove(toggleHelpTopics); if (newState == State.SUCCEEDED) { JSObject win = (JSObject) webEngine.executeScript("window");//获取浏览器引擎中的javascript语言中的window全局对象 win.setMember("app", new JavaApp());//创建一个全局对象app(类型是前文中自定义的JavaApp类型),将此app对象置入浏览器引擎中的window对象体内(window对象上下文环境中) if (needDocumentationButton) { toolBar.getChildren().add(toggleHelpTopics); } } }); // load the home page webEngine.load("http://www.oracle.com/products/index.html");//浏览器引擎初始化url加载 getChildren().add(toolBar);//添加盒子布局对象到本实例对象的根节点孩子集合上 getChildren().add(browser);//添加浏览器区域对象到本实例对象的根节点孩子集合上 } private Node createSpacer() { Region spacer = new Region(); HBox.setHgrow(spacer, Priority.ALWAYS); return spacer; } @Override protected void layoutChildren() {//浏览器页面初始化页面样式加载重写父类Region对象同名方法 double w = getWidth(); double h = getHeight(); double tbHeight = toolBar.prefHeight(w); layoutInArea(browser, 0, 0, w, h - tbHeight, 0, HPos.CENTER, VPos.CENTER); layoutInArea(toolBar, 0, h - tbHeight, w, tbHeight, 0, HPos.CENTER, VPos.CENTER); } @Override protected double computePrefWidth(double height) {//浏览器创后宽度设置重写了父类Region对象同名方法 return 900; } @Override protected double computePrefHeight(double width) { return 600; } }
____________________
import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.HPos; import javafx.geometry.VPos; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.Hyperlink; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.HBox; import javafx.scene.layout.Priority; import javafx.scene.layout.Region; import javafx.scene.paint.Color; import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; import javafx.stage.Stage; /** * @author zhaoyong * @Date 2022/10/18 * @Description */ public class WebViewTest extends Application { private Scene scene; @Override public void start(Stage stage) { // create scene stage.setTitle("Web View"); scene = new Scene(new Browser(), 900, 600, Color.web("#666970"));//场景上挂载浏览器对象new Browser() stage.setScene(scene); scene.getStylesheets().add("BrowserToolbar.css");//场景对象引用外部css文件 // show stage stage.show();//舞台展现 } public static void main(String[] args) { launch(args); } } 当用户选择了弹出菜单中的“Open Link in New Window”选项时,smallView浏览器会被添加到程序的工具栏。这个行为是通过setCreatePopupHandler方法来定义的,返回一个可选浏览器来通知应用程序在哪里绘制目标页面。程序编译后的运行结果如图