Note: Advice in this article will only work for JxBrowser 6. See the corresponding article for JxBrowser 7 here.(注意:本文中的建议仅适用于JxBrowser6,JxBrowser7相应文章请点击这里。)


To handle pop-up windows in your Swing application you have two options:(要处理Swing应用程序中的弹出窗口,您有两个选择:)


1. Register default Swing pop-up handler implementation: (1.注册默认的Swing弹出处理程序实现:)

browser.setPopupHandler(new com.teamdev.jxbrowser.chromium.swing.DefaultPopupHandler());

2. Register your own implementation where you decide how exactly you would like to display a new pop-up window (e.g. in JFrame, JWindow, application tab):  (2.注册您自己的实现,在其中您决定要如何精确地显示新的弹出窗口(例如,在JFrame,JWindow,应用程序标签中):)

browser.setPopupHandler(new PopupHandler() {
    public PopupContainer handlePopup(PopupParams params) {
        return new PopupContainer() {
            public void insertBrowser(final Browser browser, final Rectangle initialBounds) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        BrowserView browserView = new BrowserView(browser);
                        browserView.setPreferredSize(initialBounds.getSize());

                        final JFrame frame = new JFrame("Popup");
                        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                        frame.add(browserView, BorderLayout.CENTER);
                        frame.pack();
                        frame.setLocation(initialBounds.getLocation());
                        frame.setVisible(true);
                        frame.addWindowListener(new WindowAdapter() {
                            @Override
                            public void windowClosing(WindowEvent e) {
                                browser.dispose();
                            }
                        });

                        browser.addDisposeListener(new DisposeListener<Browser>() {
                            public void onDisposed(DisposeEvent<Browser> event) {
                                frame.setVisible(false);
                            }
                        });
                    }
                });
            }
        };
    }
});