使用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程序中的愿望了吧。
盡管千里冰封
依然擁有晴空
你我共同品味JAVA的濃香.
posted on 2007-10-03 08:55
千里冰封 閱讀(13699)
評論(15) 編輯 收藏 所屬分類:
JAVA開源