一、創建簡單的瀏覽器
import org.eclipse.swt.*;
import org.eclipse.swt.browser.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;public class Myswt3 {public static void main(String [] args) {Display display = new Display();final Shell shell = new Shell(display);shell.setLayout(new FillLayout());final Browser browser;try {browser = new Browser(shell, SWT.NONE);} catch (SWTError e) {System.out.println("Could not instantiate Browser: " + e.getMessage());display.dispose();return;}browser.setUrl("www.baidu.com"); //百度shell.open();while (!shell.isDisposed()) {if (!display.readAndDispatch()) display.sleep();}display.dispose();
}
上面的代碼,除了browser.setURL(“www.baidu.com”)這句之外,其他都是必備的套路代碼,可以直接粘貼復制。
我簡要介紹一下:
1.Display 用于創建可視化的瀏覽器,它相當于所有組件的父組件。
2.shell 作用應該類似于窗體,它的parent是display
3.browser的try{}catch{}塊用于創建瀏覽器,增加健壯性,其中SWT.NONE是關鍵語句,這表示默認調用系統的瀏覽器內核。不過也可以更改參數,比如SWT.Mozilla,調用火狐內核,但是這樣還需要一些其他設置,比較麻煩,所以姑且用IE內核吧。
4.最后幾行是加載和開始以及關閉釋放內存的操作,全部都是套路。
二、運行javaScript代碼
final String SCRIPT = "document.write('hello world')";browser.addProgressListener(ProgressListener.completedAdapter(event -> {browser.execute(SCRIPT);}));
將上面兩句添加進去,SCRIPT為javaScript代碼,你可以寫的很簡單,也可以寫的很復雜。
三、高級功能
這里面有一個很強大的功能是可以實現Html和java的交互,利用Javascript,可以將Html中的一些參數傳遞出來,在java的程序中進行運算,請看具體的實例
import org.eclipse.swt.*;
import org.eclipse.swt.browser.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;public class Myswt3 {public static void main(String [] args) {final String SCRIPT ="document.onmousedown = function(e) {if (!e) {e = window.event;} if (e) {mouseDown(e.clientX, e.clientY);}}";Display display = new Display();final Shell shell = new Shell(display);shell.setLayout(new FillLayout());final Browser browser;try {browser = new Browser(shell, SWT.NONE);} catch (SWTError e) {System.out.println("Could not instantiate Browser: " + e.getMessage());display.dispose();return;}browser.addProgressListener(ProgressListener.completedAdapter(event -> {final BrowserFunction function = new CustomFunction(browser, "mouseDown");browser.execute(SCRIPT);}));browser.setUrl("www.baidu.com");shell.open();while (!shell.isDisposed()) {if (!display.readAndDispatch()) display.sleep();}display.dispose();
}static class CustomFunction extends BrowserFunction {CustomFunction (Browser browser, String name) {super (browser, name);}@Overridepublic Object function (Object[] arguments) {System.out.println ("mouseDown: " + ((Number)arguments[0]).intValue() + "," + ((Number)arguments[1]).intValue());return null;}
}}
我來具體的解釋一下:
首先,在javaScript中定義了一個mousedown函數,他有兩個參數就是x,y坐標,但是在代碼中沒有具體寫這個函數的代碼,只是聲明要用到這樣一個函數,而這個函數可以在java代碼中進行完善。
定義一個CustomFunction類繼承BrowserFunction類,在類中重寫 public Object function()函數,而這個函數體就是mousedown的函數體
用于連接的語句就是:BrowserFunction function = new CustomFunction(browser, “mouseDown”);
總體來說就是,
1.javaScript中只簡單聲明函數和參數
2.以這個函數的名稱創建一個BrowserFunction對象
3.覆寫BrowserFunction中的public Object function 函數,object arguments 是你自己定義的函數的要傳入的參數,比如上例arguments[0],就時clientX,arguments[1]就是clientY,然后在內部具體的實現這個函數,最后加上return。
通過這個過程,將網頁中的clientX,和clientY傳到了java代碼中進行了操作。
所以我們可以利用這個方法來實現更多的功能。
四、繼續擴展
除了上面的方法進行交互外還有一種簡單的方法,可以用更短的語句實現更多的功能。
browser.evaluate("return document.getElementById('myid').childNodes[0].nodeValue;")
可以使用evaluate函數直接返回值,其中return document.getElementById(‘myid’).childNodes[0].nodeValue;是JavaScript代碼。