Java-Swing嵌入浏览器
来源:互联网 发布:php工程师岗位职责 编辑:程序博客网 时间:2024/06/05 13:45
今天要说的额是浏览器的第一个版本是用DJnative-swt和swt包开发的调用本地浏览器和webkit浏览器的示例
这是我的工程目录【源码见最后】:
src下为写的源码,lib为引入的swt和DJnative和mozilla接口包~
我们来看两个类,此两个类是嵌入webkiet mozilla的内核浏览器 要用到xulrunner
这里有一句代码 就是下面这句
//指定xulRunner路径 如不指定就调用系统注册的xulrunner 注册为 xulRunner目录下的xulrunner --register-global NSSystemPropertySWT.WEBBROWSER_XULRUNNER_HOME.set(System.getProperty("user.dir") + "/xulrunner");
如果没有写这句代码引入工程下的xulrunner,那么就必须要进行注册注册为xulrunner --register-global 卸载为 xulrunner --unregister-global
XPCOMDownloadManager.java 类 调用mozilla内核浏览器
/* * luwenbin006@163.com (luwenbin006@163.com) * http://www.luwenbin.com * * See the file "readme.txt" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. */package com.luwenbin.webbrowser;import java.awt.BorderLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.JComponent;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.SwingUtilities;import org.mozilla.interfaces.nsICancelable;import org.mozilla.interfaces.nsIComponentRegistrar;import org.mozilla.interfaces.nsIFactory;import org.mozilla.interfaces.nsILocalFile;import org.mozilla.interfaces.nsIMIMEInfo;import org.mozilla.interfaces.nsIRequest;import org.mozilla.interfaces.nsISupports;import org.mozilla.interfaces.nsITransfer;import org.mozilla.interfaces.nsIURI;import org.mozilla.interfaces.nsIWebProgress;import org.mozilla.interfaces.nsIWebProgressListener;import org.mozilla.xpcom.Mozilla;import chrriis.common.UIUtils;import chrriis.dj.nativeswing.swtimpl.NSSystemPropertySWT;import chrriis.dj.nativeswing.swtimpl.NativeInterface;import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;import chrriis.dj.nativeswing.swtimpl.components.MozillaXPCOM;/** * @author luwenbin006@163.com */public class XPCOMDownloadManager{ public static JComponent createContent() { JPanel contentPane = new JPanel(new BorderLayout()); JPanel webBrowserPanel = new JPanel(new BorderLayout()); webBrowserPanel.setBorder(BorderFactory.createTitledBorder("Native Web Browser component")); //指定xulRunner路径 如不指定就调用系统注册的xulrunner 注册为 xulRunner目录下的xulrunner --register-global NSSystemPropertySWT.WEBBROWSER_XULRUNNER_HOME.set(System.getProperty("user.dir") + "/xulrunner"); final JWebBrowser webBrowser = new JWebBrowser(JWebBrowser.useXULRunnerRuntime()); webBrowser.navigate("http://www.eclipse.org/downloads"); webBrowserPanel.add(webBrowser, BorderLayout.CENTER); contentPane.add(webBrowserPanel, BorderLayout.CENTER); // Create an additional area to see the downloads in progress. final JPanel downloadsPanel = new JPanel(new GridLayout(0, 1)); downloadsPanel.setBorder(BorderFactory.createTitledBorder("Download manager (on-going downloads are automatically added to this area)")); contentPane.add(downloadsPanel, BorderLayout.SOUTH); // We can only access XPCOM when it is properly initialized. // This happens when the web browser is created so we run our code in sequence. webBrowser.runInSequence(new Runnable() { public void run() { try { nsIComponentRegistrar registrar = MozillaXPCOM.Mozilla.getComponentRegistrar(); String NS_DOWNLOAD_CID = "e3fa9D0a-1dd1-11b2-bdef-8c720b597445"; String NS_TRANSFER_CONTRACTID = "@mozilla.org/transfer;1"; registrar.registerFactory(NS_DOWNLOAD_CID, "Transfer", NS_TRANSFER_CONTRACTID, new nsIFactory() { public nsISupports queryInterface(String uuid) { if(uuid.equals(nsIFactory.NS_IFACTORY_IID) || uuid.equals(nsIFactory.NS_ISUPPORTS_IID)) { return this; } return null; } public nsISupports createInstance(nsISupports outer, String iid) { return createTransfer(downloadsPanel); } public void lockFactory(boolean lock) {} }); } catch(Exception e) { JOptionPane.showMessageDialog(webBrowser, "Failed to register XPCOM download manager.\nPlease check your XULRunner configuration.", "XPCOM interface", JOptionPane.ERROR_MESSAGE); return; } } }); return contentPane; } private static nsITransfer createTransfer(final JPanel downloadsPanel) { return new nsITransfer() { public nsISupports queryInterface(String uuid) { if(uuid.equals(nsITransfer.NS_ITRANSFER_IID) || uuid.equals(nsITransfer.NS_IWEBPROGRESSLISTENER2_IID) || uuid.equals(nsITransfer.NS_IWEBPROGRESSLISTENER_IID) || uuid.equals(nsITransfer.NS_ISUPPORTS_IID)) { return this; } return null; } private JComponent downloadComponent; private JLabel downloadStatusLabel; private String baseText; public void init(nsIURI source, nsIURI target, String displayName, nsIMIMEInfo MIMEInfo, double startTime, nsILocalFile tempFile, final nsICancelable cancelable) { downloadComponent = new JPanel(new BorderLayout(5, 5)); downloadComponent.setBorder(BorderFactory.createEmptyBorder(2, 5, 2, 5)); JButton cancelDownloadButton = new JButton("Cancel"); downloadComponent.add(cancelDownloadButton, BorderLayout.WEST); final String path = target.getPath(); cancelDownloadButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { cancelable.cancel(Mozilla.NS_ERROR_ABORT); removeDownloadComponent(); new File(path + ".part").delete(); } }); baseText = "Downloading to " + path; downloadStatusLabel = new JLabel(baseText); downloadComponent.add(downloadStatusLabel, BorderLayout.CENTER); downloadsPanel.add(downloadComponent); downloadsPanel.revalidate(); downloadsPanel.repaint(); } public void onStateChange(nsIWebProgress webProgress, nsIRequest request, long stateFlags, long status) { if((stateFlags & nsIWebProgressListener.STATE_STOP) != 0) { removeDownloadComponent(); } } private void removeDownloadComponent() { downloadsPanel.remove(downloadComponent); downloadsPanel.revalidate(); downloadsPanel.repaint(); } public void onProgressChange64(nsIWebProgress webProgress, nsIRequest request, long curSelfProgress, long maxSelfProgress, long curTotalProgress, long maxTotalProgress) { long currentKBytes = curTotalProgress / 1024; long totalKBytes = maxTotalProgress / 1024; downloadStatusLabel.setText(baseText + " (" + currentKBytes + "/" + totalKBytes + ")"); } public void onStatusChange(nsIWebProgress webProgress, nsIRequest request, long status, String message) {} public void onSecurityChange(nsIWebProgress webProgress, nsIRequest request, long state) {} public void onProgressChange(nsIWebProgress webProgress, nsIRequest request, int curSelfProgress, int maxSelfProgress, int curTotalProgress, int maxTotalProgress) {} public void onLocationChange(nsIWebProgress webProgress, nsIRequest request, nsIURI location) {} }; } /* Standard main method to try that test as a standalone application. */ public static void main(String[] args) { NativeInterface.open(); UIUtils.setPreferredLookAndFeel(); SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new JFrame("DJ Native Swing Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(createContent(), BorderLayout.CENTER); frame.setSize(800, 600); frame.setLocationByPlatform(true); frame.setVisible(true); } }); NativeInterface.runEventPump(); }}
下面是运行效果:
XPCOMToggleEditionModer.java 类 调用mozilla内核浏览器 并启用编辑模式
/* * luwenbin006@163.com (luwenbin006@163.com) * http://www.luwenbin.com * * See the file "readme.txt" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. */package com.luwenbin.webbrowser;import java.awt.BorderLayout;import java.awt.FlowLayout;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import javax.swing.BorderFactory;import javax.swing.JCheckBox;import javax.swing.JComponent;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.SwingUtilities;import org.mozilla.interfaces.nsIDOMDocument;import org.mozilla.interfaces.nsIDOMNSHTMLDocument;import org.mozilla.interfaces.nsIDOMWindow;import org.mozilla.interfaces.nsIWebBrowser;import chrriis.common.UIUtils;import chrriis.dj.nativeswing.swtimpl.NSSystemPropertySWT;import chrriis.dj.nativeswing.swtimpl.NativeInterface;import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;import chrriis.dj.nativeswing.swtimpl.components.MozillaXPCOM;/** * @author luwenbin006@163.com */public class XPCOMToggleEditionMode{ public static JComponent createContent() { JPanel contentPane = new JPanel(new BorderLayout()); JPanel webBrowserPanel = new JPanel(new BorderLayout()); webBrowserPanel.setBorder(BorderFactory.createTitledBorder("Native Web Browser component")); //指定xulRunner路径 如不指定就调用系统注册的xulrunner 注册为 xulRunner目录下的xulrunner --register-global NSSystemPropertySWT.WEBBROWSER_XULRUNNER_HOME.set(System.getProperty("user.dir") + "/xulrunner"); final JWebBrowser webBrowser = new JWebBrowser(JWebBrowser.useXULRunnerRuntime()); webBrowser.navigate("http://www.google.com"); webBrowserPanel.add(webBrowser, BorderLayout.CENTER); contentPane.add(webBrowserPanel, BorderLayout.CENTER); // Create an additional bar allowing to toggle the edition mode of the web browser. JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 4, 4)); JCheckBox designModeCheckBox = new JCheckBox("Edition Mode (allows to type text or resize elements directly in the page)"); designModeCheckBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { nsIWebBrowser iWebBrowser = MozillaXPCOM.getWebBrowser(webBrowser); if(iWebBrowser == null) { JOptionPane.showMessageDialog(webBrowser, "The XPCOM nsIWebBrowser interface could not be obtained.\nPlease check your XULRunner configuration.", "XPCOM interface", JOptionPane.ERROR_MESSAGE); return; } nsIDOMWindow window = iWebBrowser.getContentDOMWindow(); nsIDOMDocument document = window.getDocument(); nsIDOMNSHTMLDocument nsDocument = (nsIDOMNSHTMLDocument)document.queryInterface(nsIDOMNSHTMLDocument.NS_IDOMNSHTMLDOCUMENT_IID); nsDocument.setDesignMode(e.getStateChange() == ItemEvent.SELECTED ? "on" : "off"); } }); buttonPanel.add(designModeCheckBox); contentPane.add(buttonPanel, BorderLayout.SOUTH); return contentPane; } /* Standard main method to try that test as a standalone application. */ public static void main(String[] args) { NativeInterface.open(); UIUtils.setPreferredLookAndFeel(); SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new JFrame("DJ Native Swing Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(createContent(), BorderLayout.CENTER); frame.setSize(800, 600); frame.setLocationByPlatform(true); frame.setVisible(true); } }); NativeInterface.runEventPump(); }}
下面是运行效果:
SimpleWebBrowserExample.java 调用本机默认浏览器
/* * luwenbin006@163.com (luwenbin006@163.com) * http://www.luwenbin.com * * See the file "readme.txt" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. */package com.luwenbin.webbrowser;import java.awt.BorderLayout;import java.awt.FlowLayout;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import javax.swing.BorderFactory;import javax.swing.JCheckBox;import javax.swing.JComponent;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.SwingUtilities;import chrriis.common.UIUtils;import chrriis.dj.nativeswing.swtimpl.NativeInterface;import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;/** * @author luwenbin006@163.com */public class SimpleWebBrowserExample{ public static JComponent createContent() { JPanel contentPane = new JPanel(new BorderLayout()); JPanel webBrowserPanel = new JPanel(new BorderLayout()); webBrowserPanel.setBorder(BorderFactory.createTitledBorder("Native Web Browser component")); final JWebBrowser webBrowser = new JWebBrowser(); webBrowser.navigate("http://www.google.com"); webBrowserPanel.add(webBrowser, BorderLayout.CENTER); contentPane.add(webBrowserPanel, BorderLayout.CENTER); // Create an additional bar allowing to show/hide the menu bar of the web browser. JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 4, 4)); JCheckBox menuBarCheckBox = new JCheckBox("Menu Bar", webBrowser.isMenuBarVisible()); menuBarCheckBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { webBrowser.setMenuBarVisible(e.getStateChange() == ItemEvent.SELECTED); } }); buttonPanel.add(menuBarCheckBox); contentPane.add(buttonPanel, BorderLayout.SOUTH); return contentPane; } /* Standard main method to try that test as a standalone application. */ public static void main(String[] args) { NativeInterface.open(); UIUtils.setPreferredLookAndFeel(); SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new JFrame("DJ Native Swing Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(createContent(), BorderLayout.CENTER); frame.setSize(800, 600); frame.setLocationByPlatform(true); frame.setVisible(true); } }); NativeInterface.runEventPump(); }}
下面是运行效果:
--------------------------------------------------------------------
放出源码下载地址【鄙视伸手党】:http://pan.baidu.com/s/1mgmjgso
--------------------------------------------------------------------
这是qtjambi利用webview来做嵌入式浏览器,下面是我的工程目录。
运行效果如下图:
代码相关:
package qtBowers;import com.trolltech.qt.core.QUrl;import com.trolltech.qt.gui.*;import com.trolltech.qt.webkit.QWebView;public class Bowers extends QWidget{ Ui_Bowers ui = new Ui_Bowers(); public static void main(String[] args) { QApplication.initialize(args); QWebView webview = new QWebView(); webview.load(new QUrl("http://www.baidu.com")); webview.show(); QApplication.exec(); } public Bowers() { ui.setupUi(this); } public Bowers(QWidget parent) { super(parent); ui.setupUi(this); }}
--------------------------------------------------------------------
这个源码我就不放了,就一个类,自己去这里去下载qt-jambi版本 :http://sourceforge.net/projects/qtjambi/files/
qt-jambi eclipse插件就只出到了4.5.2 需要qt-jambi4.5.2版本才行哦 插件地址:http://pan.baidu.com/s/1eQnHtpK
--------------------------------------------------------------------原文链接:http://www.cnblogs.com/luwenbin/p/4125028.html
- Java Swing嵌入浏览器
- java----swing嵌入浏览器
- Java-Swing嵌入浏览器
- 转:Java-Swing嵌入浏览器
- swing和java里嵌入浏览器
- swing和java里嵌入浏览器
- java swing 打开浏览器
- java swing内嵌浏览器
- java swing内嵌浏览器
- 在java应用程序中嵌入浏览器
- java应用程序中嵌入浏览器总结
- 在java应用程序中嵌入浏览器
- java应用程序中嵌入浏览器总结
- java应用程序中嵌入浏览器错误总结
- Java Swing点击按钮启动浏览器示例
- java swing实现简单的图片浏览器
- Swing嵌入Html/Jsp
- SWT中嵌入Swing
- linux查找命令
- css3中强大的rem单位
- test
- 深度剖析什么是 SLI、SLO和SLA?
- List转Map
- Java-Swing嵌入浏览器
- 安装Anaconda之前,有的已经安装过一个Python版本
- HTTP返回码总结
- 解题报告:HDU_6128:Inverse of sum (二次剩余)
- Spire.Email 教程:从C#,VB.NET中的PST文件获取文件夹信息
- String的按值传递,java传参都是传值
- ShaderLab Syntax
- DotNetCore跨平台~在appsettings.json里自定义配置项
- vim 搜索后大小写替换