今天要说的额是浏览器的第一个版本是用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. HW-找7(测试ok满分注意小于等于30000的条件)

    输出7有关数字的个数,包括7的倍数,还有包含7的数字(如17,27,37...70,71,72,73...)的个数 知识点 循环 运行时间限制 0M 内存限制 0 输入 一个正整数N.(N不大于300 ...

  2. ### 学习《C++ Primer》- 6

    Part 6: 拷贝控制(第13章) // @author: gr // @date: 2015-01-08 // @email: forgerui@gmail.com 一.拷贝.赋值与销毁 拷贝构造 ...

  3. Json字符与Json对象的相互转换

    Json字符与Json对象的相互转换方式有很多,接下来将为大家一一介绍下,感兴趣的朋友可以参考下哈,希望可以帮助到你 1>jQuery插件支持的转换方式: 复制代码 代码如下: $.parseJ ...

  4. 蝇量模式(Flyweight Pattern)

    蝇量模式:让某个类的一个实例能用来提供许多“虚拟实例”. 在有大量对象时,有可能造成内存溢出,把其中共同的部分抽象出来,如果有相同的业务请求,直接返回在内存中已有的对象,避免重复创建.(JAVA中的S ...

  5. Jquery-Mobile滚动事件

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> < ...

  6. 微软自带iscsi客户端对iqn的要求

    节点名称:Microsoft iSCSI 发起程序严格遵守为 iSCSI 节点名称指定的规则.这些规则也适用于 Microsoft iSCSI 发起程序节点名称以及发现的任何目标节点名称.构建 iSC ...

  7. 用Objective-C的foundation框架解决表达式求值问题

    主要思想: 本程序分2个类 一个是ExpressionString类,主要用于存储表达式以及对它进行求值.以下是该类中的内容: (NSString *)expString//用于存储要计算的表达式: ...

  8. yii2 用gii生成后台模块 view path描述

    view path 格式: @backend/views/refund , 注意@和/

  9. vertical-align:middle图片或者按钮垂直居中

    <img>或者button按钮 垂直不对齐,加上vertical-align:middle,就能垂直对齐,常用于水平布局的验证码图片 或者按钮 也适用于 text和button在一起也会不 ...

  10. 安装JDK设置环境变量

    PS:之前在CSDN上写的文章,现在转到博客园~ 在安装过程中第一次让选择jdk的安装路径,第二次让选择jre的安装路径.两者不可以在同一个文件夹下,否则在cmd中运行javac时会报:摘不到或无法加 ...