今天要说的额是浏览器的第一个版本是用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

Java-Swing嵌入浏览器(一)的更多相关文章

  1. swing 嵌入浏览器

    需求要在swing加一个浏览器,在网上找了一个挺方便的方法,现在把代码贴上来 力求方便. package com.vtradex.page.shipment; import static javafx ...

  2. Java Swing 第01记 Hello Word

    首先来一个Java Swing的HelloWord程序. package cn.java.swing.chapter03; import javax.swing.JButton; import jav ...

  3. Java Swing的进化

    摘 要:Swing已是一个比较老的工具集了,在美观的用户界面出来之前需要开发很长时间.它缺少一些你在开发富UI时所需的组件.幸运地是,像 Substance,SwingX及Java Look-and_ ...

  4. 客户端是选择Java Swing还是C# Winform

      登录|注册     mentat的专栏       目录视图 摘要视图 订阅 [专家问答]韦玮:Python基础编程实战专题     [知识库]Swift资源大集合    [公告]博客新皮肤上线啦 ...

  5. 一步一步写出java swing登录界面,以及输入的参数获取

    经过好几天的学习,研究,接下来说说java swing,以及内嵌浏览器的方法. 一.swing是一个用于java应用程序用户界面的的开发工具包. 例如:接下来我们做个登录界面,简要说明 做之前的构想图 ...

  6. java Swing组件和事件处理(二)

    1.BoxLayout类可以创建一个布局对象,成为盒式布局,BoxLayout在javax.Swing  border 包中,java.swing 包提供一个Box类,该类也是一个类,创建的容器称作一 ...

  7. atitit.D&D drag&drop拖拽文件到界面功能 html5 web 跟个java swing c#.net c++ 的总结

    atitit.D&D drag&drop拖拽文件到界面功能 html5 web 跟个java swing c#.net c++ 的总结 1. DND的操作流程 1 2. Html5 注 ...

  8. Java Swing interview

    http://www.careerride.com/Swing-AWT-Interview-Questions.aspx   Swing interview questions and answers ...

  9. Java Swing 第03记 布局管理器

    几种Swing常用的布局管理器 BorderLaout 它将容器分为5个部分,即东.南.西.北.中,每一个区域可以容纳一个组件,使用的时候也是通过BorderLayout中5个方位常量来确定组件所在的 ...

  10. Java swing项目-图书管理系统(swing+mysql+jdbc) 总结

    (一)java Swing的学习. (1)学习如何安装windowbuilder插件的安装. <1>在eclipse中点击help <2>在help的下拉选中选择install ...

随机推荐

  1. ### Paper about Event Detection

    Paper about Event Detection. #@author: gr #@date: 2014-03-15 #@email: forgerui@gmail.com 看一些相关的论文. 1 ...

  2. C# 匿名类型

    c#3.0引入匿名类型,其由编译器动态生成而非显式定义. using System;using System.Collections.Generic;using System.Linq;using S ...

  3. linux 下使用crontab 定时打包日志并删除已被打包的日志

    crontab是和用户相关的,每个用户有自己对应的crontab . cron是Linux下的定时执行工具,以下是重启/关闭等等的命令 #/sbin/service crond start //启动服 ...

  4. 简单模拟java动态动态代理机制的底层实现原理

    在网上学习了马士兵老师的设计模式视屏,过程中也有认真的做相应的笔记.在次分享我的一些成果,方便大家的进一步学习. 1.接口  } 2.被代理的对象 public class Tank implemen ...

  5. MySQL二进制文件规范安装

    演示环境介绍 操作系统:CentOS 6.7  (64位) 二进制包:mysql-5.6.30-linux-glibc2.5-x86_64.tar.gz MySQL 下载地址:http://dev.m ...

  6. Qt多文档界面应用设计

    使用Qt编写多文档界面(MDI)应用相当方便,主要会使用到QMdiArea和QMdiSubWindow两个类.可以查看Qt Asistant中这两个类的说明文档,里面介绍的相当详细.另外,可以搜索例程 ...

  7. 运行phpize失败排查

    ==相关参考== rpm包 http://rpmfind.net/linux/rpm2html/ phpize学习 ==问题及排查过程== 1.phpize失败 2.yum install php-d ...

  8. less的使用方法

    LESS学习 1.变量 我们可以把一个css样式的值赋给一个参数,然后再设置样式的时候只需要设置这个参数名,如果要修改,就改参数的值就可以了. demo.html <!DOCTYPE html& ...

  9. Ubuntu Vim YouCompleteMe 安装

    0. 必要工具安装 sudo apt-get install build-essential cmake 1. 安装 vundle mkdir ~/.vim/bundle git clone http ...

  10. An error occurred while collecting items to be installed session context was:(profile=DefaultProfile... 解决方案

    遇到同样问题的小伙伴请:点击Eclipse上方工具栏中help --> Install new software... --> 看图 点击进红框的位置在打开的窗口中,将窗口右侧的Avail ...