java 嵌入网页_Java-Swing嵌入浏览器(一)

本文展示了如何使用Java Swing创建一个应用程序,并集成一个网页浏览器组件。通过示例代码,演示了如何利用XULRunner加载网页,并提供了一个简单的下载管理器,可以展示下载进度。
摘要由CSDN通过智能技术生成

/** 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.*/

packagecom.luwenbin.webbrowser;importjava.awt.BorderLayout;importjava.awt.GridLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.File;importjavax.swing.BorderFactory;importjavax.swing.JButton;importjavax.swing.JComponent;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JOptionPane;importjavax.swing.JPanel;importjavax.swing.SwingUtilities;importorg.mozilla.interfaces.nsICancelable;importorg.mozilla.interfaces.nsIComponentRegistrar;importorg.mozilla.interfaces.nsIFactory;importorg.mozilla.interfaces.nsILocalFile;importorg.mozilla.interfaces.nsIMIMEInfo;importorg.mozilla.interfaces.nsIRequest;importorg.mozilla.interfaces.nsISupports;importorg.mozilla.interfaces.nsITransfer;importorg.mozilla.interfaces.nsIURI;importorg.mozilla.interfaces.nsIWebProgress;importorg.mozilla.interfaces.nsIWebProgressListener;importorg.mozilla.xpcom.Mozilla;importchrriis.common.UIUtils;importchrriis.dj.nativeswing.swtimpl.NSSystemPropertySWT;importchrriis.dj.nativeswing.swtimpl.NativeInterface;importchrriis.dj.nativeswing.swtimpl.components.JWebBrowser;importchrriis.dj.nativeswing.swtimpl.components.MozillaXPCOM;/***@authorluwenbin006@163.com*/

public classXPCOMDownloadManager

{public staticJComponent createContent()

{

JPanel contentPane= new JPanel(newBorderLayout());

JPanel webBrowserPanel= new JPanel(newBorderLayout());

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 = newJWebBrowser(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(newRunnable()

{public voidrun()

{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, newnsIFactory()

{publicnsISupports queryInterface(String uuid)

{if(uuid.equals(nsIFactory.NS_IFACTORY_IID) ||uuid.equals(nsIFactory.NS_ISUPPORTS_IID))

{return this;

}return null;

}publicnsISupports createInstance(nsISupports outer, String iid)

{returncreateTransfer(downloadsPanel);

}public void lockFactory(booleanlock) {}

});

}catch(Exception e)

{

JOptionPane.showMessageDialog(webBrowser,"Failed to register XPCOM download manager.\nPlease check your XULRunner configuration.", "XPCOM interface", JOptionPane.ERROR_MESSAGE);return;

}

}

});returncontentPane;

}private static nsITransfer createTransfer(finalJPanel downloadsPanel)

{return newnsITransfer()

{publicnsISupports 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;

}privateJComponent downloadComponent;privateJLabel downloadStatusLabel;privateString baseText;public void init(nsIURI source, nsIURI target, String displayName, nsIMIMEInfo MIMEInfo, double startTime, nsILocalFile tempFile, finalnsICancelable 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(newActionListener()

{public voidactionPerformed(ActionEvent e)

{

cancelable.cancel(Mozilla.NS_ERROR_ABORT);

removeDownloadComponent();new File(path + ".part").delete();

}

});

baseText= "Downloading to " +path;

downloadStatusLabel= newJLabel(baseText);

downloadComponent.add(downloadStatusLabel, BorderLayout.CENTER);

downloadsPanel.add(downloadComponent);

downloadsPanel.revalidate();

downloadsPanel.repaint();

}public void onStateChange(nsIWebProgress webProgress, nsIRequest request, long stateFlags, longstatus)

{if((stateFlags & nsIWebProgressListener.STATE_STOP) != 0)

{

removeDownloadComponent();

}

}private voidremoveDownloadComponent()

{

downloadsPanel.remove(downloadComponent);

downloadsPanel.revalidate();

downloadsPanel.repaint();

}public void onProgressChange64(nsIWebProgress webProgress, nsIRequest request, long curSelfProgress, long maxSelfProgress, long curTotalProgress, longmaxTotalProgress)

{long currentKBytes = curTotalProgress / 1024;long totalKBytes = maxTotalProgress / 1024;

downloadStatusLabel.setText(baseText+ " (" + currentKBytes + "/" + totalKBytes + ")");

}public void onStatusChange(nsIWebProgress webProgress, nsIRequest request, longstatus, String message) {}public void onSecurityChange(nsIWebProgress webProgress, nsIRequest request, longstate) {}public void onProgressChange(nsIWebProgress webProgress, nsIRequest request, int curSelfProgress, int maxSelfProgress, int curTotalProgress, intmaxTotalProgress) {}public voidonLocationChange(nsIWebProgress webProgress, nsIRequest request, nsIURI location) {}

};

}/*Standard main method to try that test as a standalone application.*/

public static voidmain(String[] args)

{

NativeInterface.open();

UIUtils.setPreferredLookAndFeel();

SwingUtilities.invokeLater(newRunnable()

{public voidrun()

{

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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值