利用jdic实现网页浏览器

  使用VC,VB或者C#的开发者们对于在程序里面嵌入一个网页来说,那真是小事一桩。但是在JAVA里面,却几乎是不可能实现的任务。JEditorPane虽然说可以打开网页,但是它那解析速度以及解析质量,对于今天日益复杂的网页内容来说,就像没有一样。今天我们就使用一个开源的组件(jdic)来实现在JAVA程序里面嵌入网页的效率。

  下面言归正转吧,我们来介绍一下这个开源的组件,它的名字叫JDIC(JDesktop Integration Components),网址为:https://jdic.dev.java.net/,它提供了一种访问桌面组件的API,其中JDK6.0就采纳了其中了一些,比如系统栏图标的SystemTray和SystemIcon,还有代表桌面的Desktop等等,可见这个API是挺不错的。由于网页浏览器的特殊性,标准的JDK并没有把它加入进来,但是我们一样可以下载它来使用这个功能。明显地,这个功能是用本地方法实现的,所以下载完以后,把jdic.dll放到我们的path目录中,比如system32文件夹下面,然后我们就可以使用它的功能从而增加我们的JAVA程序了。

  上面的例子代码如下:

/*
* Test1.java
*
* Created on 2007-10-2, 17:29:30
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package test2;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.jdesktop.jdic.browser.IWebBrowser;
import org.jdesktop.jdic.browser.WebBrowser;
import org.jdesktop.jdic.browser.WebBrowserEvent;
import org.jdesktop.jdic.browser.WebBrowserListenerAdapter;

/**
*
* @author hadeslee
*/
public class Test1 extends JPanel implements ActionListener {

 private JTextField input;
 private JButton go;
 private IWebBrowser web;

 public Test1() {
  super(new BorderLayout());
  initWindow();
 }

 private void initWindow() {
  try {
   web = new WebBrowser();
   web.addWebBrowserListener(new MyListener());
   go = new JButton("转到");
   input = new JTextField();
   JPanel up = new JPanel(new BorderLayout());
   up.add(input, BorderLayout.CENTER);
   up.add(go, BorderLayout.EAST);
   this.add(up, BorderLayout.NORTH);
   this.add(web.asComponent(), BorderLayout.CENTER);
   input.addActionListener(this);
   go.addActionListener(this);
  } catch (Exception ex) {
   Logger.getLogger(Test1.class.getName()).log(Level.SEVERE, null, ex);
  }
  JFrame jf = new JFrame("JAVA浏览器");
  jf.add(this, BorderLayout.CENTER);
  jf.setSize(500, 300);
  jf.setLocationRelativeTo(null);
  jf.setVisible(true);
  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }

 public void actionPerformed(ActionEvent ae) {
  doOpen();
 }

 private void doOpen() {
  try {
   String text = input.getText();
   if (text == null || text.equals("")) {
    return;
   }
   if (!text.toLowerCase().startsWith("http://")) {
    text = "http://" + text;
   }
   web.setURL(new URL(text));
  } catch (MalformedURLException ex) {
   Logger.getLogger(Test1.class.getName()).log(Level.SEVERE, null, ex);
  }
 }

 public static void main(String[] args) {
  new Test1();
 }

 private class MyListener extends WebBrowserListenerAdapter {

  private MyListener() {}

  @Override
  public void documentCompleted(WebBrowserEvent arg0) {
   System.out.println("文档下载完。。。");
   web.executeScript("alert('文档下载完毕!')");
   // web.setContent("<html><H1>Hello world!!<H1>" +
   // "<a href=http://www.google.cn>点我</a></html>");
   // web.removeWebBrowserListener(this);
  }
 }
}

  它比一般的别的实现好的地方就是,它可以很完全地和JAVA代码进行交互,包括浏览器事件的监听,浏览器内容的获取,以及自己调用浏览器来执行一段javasript,这些都是很强大并且很实用的功能。

  怎么样,这下满足了一下我们把网页嵌入到JAVA程序中的愿望了吧。

  1. da shang
    donate-alipay
               donate-weixin weixinpay

发表评论↓↓