利用Java實現網頁瀏覽器

來源:互聯網
上載者:User

導讀:
  使用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("
Hello world!!
" +
  // "點我");
  // web.removeWebBrowserListener(this);
  }
  }
  }
  它比一般的別的實現好的地方就是,它可以很完全地和JAVA代碼進行互動,包括瀏覽器事件的監聽,瀏覽器內容的擷取,以及自己呼叫瀏覽器來執行一段javasript,這些都是很強大並且很實用的功能。
  怎麼樣,這下滿足了一下我們把網頁嵌入到JAVA程式中的願望了吧。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.