基于JxBrowser7.19实现封装网页到Java Swing中(包含对界面跳转和新窗口开启的监听功能)

直接上代码.

package xxxxxx;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

import com.teamdev.jxbrowser.browser.Browser;
import com.teamdev.jxbrowser.browser.callback.OpenPopupCallback;
import com.teamdev.jxbrowser.browser.callback.OpenPopupCallback.Response;
import com.teamdev.jxbrowser.engine.Engine;
import com.teamdev.jxbrowser.engine.EngineOptions;
import com.teamdev.jxbrowser.engine.RenderingMode;
import com.teamdev.jxbrowser.navigation.event.FrameLoadFinished;
import com.teamdev.jxbrowser.navigation.event.NavigationFinished;
import com.teamdev.jxbrowser.view.swing.BrowserView;

public class Browser7x {
	private static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

	public static void main(String[] args) {
		Engine engine = Engine.newInstance(
                EngineOptions.newBuilder(RenderingMode.HARDWARE_ACCELERATED).
                licenseKey("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").build());
							//这是证书密钥,去官网或其他地方(如:Github)去找或买

        Browser browser = engine.newBrowser();
        browser.set(OpenPopupCallback.class, params -> {
            SwingUtilities.invokeLater(() -> {
            	Browser popup = params.popupBrowser();
	            BrowserView newView = BrowserView.newInstance(popup);

	            popup.navigation().on(FrameLoadFinished.class, event -> {
	                String url = (event.frame().browser()).url();
	                System.out.println("新窗口打开: " + url);
	            });

	            JFrame newFrame = new JFrame();
                newFrame.setSize(800, 600);
                newFrame.setLocationRelativeTo(null);
                newFrame.getContentPane().add(newView, BorderLayout.CENTER);
                newFrame.addWindowListener(new WindowAdapter() {
	    			public void windowClosing(WindowEvent e) {
	    				popup.close();
	    				newFrame.dispose();
	    			}
	    		});
                newFrame.setVisible(true);
            });
            return Response.proceed();
        });

        browser.navigation().on(NavigationFinished.class, event -> {
            String url = (event.frame().browser()).url();
            System.out.println("Navigation finished: " + url);
        });

        BrowserView view = BrowserView.newInstance(browser);
        JScrollPane scrollPane = new JScrollPane(view);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setOpaque(false);
        scrollPane.getViewport().setOpaque(false);
        scrollPane.setBorder(null);

        JFrame jf = new JFrame();
	    jf.getContentPane().add(scrollPane);
	    jf.setSize(screenSize.width, screenSize.height);
	    jf.setUndecorated(true);
	    jf.setResizable(false);
	    jf.setVisible(true);

        browser.navigation().loadUrl("www.baidu.com");
    }
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值