在許多平台中,Browser控制項皆被作為一個必需的控制項給出,並提供了DOM介面,用於訪問Browser的內容,相對來說,SWT中的Browser控制項就比較薄弱,沒有提供DOM的可控制介面。
那麼,如何和控制項所載入的頁面進行互動呢?比如需要在整合web應用的環境中實現類比登陸、自動填表等功能。
SWT中對Browser有不同的實現,目前實現的有IE和Mozilla。在Browser的建構函式中根據不同的平台和不同的style設定類決定使用哪個類的實現。 org.eclipse.swt.browser.Mozilla org.eclipse.swt.browser.IE 是已經實現的,而其他的 org.eclipse.swt.browser.Safari org.eclipse.swt.browser.Voyager 則沒有實現。
Java應用程式如何把本地的IE或FIREFOX 嵌入到程式中。。。如果你用swt,可以用Browser類。
如果是awt或者swing,最好自己寫個控制項來做這個事情,寫一個java的視窗,
然後用com和IE通訊,用XPCOM和Firefox通訊
下面一個簡單的Swt例子。
代碼如下:
import org.eclipse.swt.SWT;<br />import org.eclipse.swt.browser.Browser;<br />import org.eclipse.swt.layout.FormAttachment;<br />import org.eclipse.swt.layout.FormData;<br />import org.eclipse.swt.layout.FormLayout;<br />import org.eclipse.swt.widgets.Button;<br />import org.eclipse.swt.widgets.Display;<br />import org.eclipse.swt.widgets.Menu;<br />import org.eclipse.swt.widgets.MenuItem;<br />import org.eclipse.swt.widgets.Shell;<br />import org.eclipse.swt.widgets.Text;</p><p>public class TestBro {</p><p>private Text text;<br />protected Shell shell;</p><p>/**<br /> * Launch the application<br /> * @param args<br /> */<br />public static void main(String[] args) {<br />try {<br />TestBro window = new TestBro();<br />window.open();<br />} catch (Exception e) {<br />e.printStackTrace();<br />}<br />}</p><p>/**<br /> * Open the window<br /> */<br />public void open() {<br />final Display display = Display.getDefault();<br />createContents();<br />shell.open();<br />shell.layout();<br />while (!shell.isDisposed()) {<br />if (!display.readAndDispatch())<br />display.sleep();<br />}<br />}</p><p>/**<br /> * Create contents of the window<br /> */<br />protected void createContents() {<br />shell = new Shell();<br />shell.setLayout(new FormLayout());<br />shell.setSize(500, 375);<br />shell.setText("SWT Application");</p><p>final Browser browser = new Browser(shell, SWT.NONE);<br />final FormData fd_browser = new FormData();<br />fd_browser.bottom = new FormAttachment(100, 0);<br />fd_browser.right = new FormAttachment(100, 0);<br />fd_browser.top = new FormAttachment(0, 31);<br />fd_browser.left = new FormAttachment(0, 0);<br />browser.setLayoutData(fd_browser);<br />browser.setUrl("http://blog.csdn.net/hytdsky");</p><p>final Menu menu = new Menu(shell, SWT.BAR);<br />shell.setMenuBar(menu);</p><p>final MenuItem newSubmenuMenuItem = new MenuItem(menu, SWT.CASCADE);<br />newSubmenuMenuItem.setText("檔案");</p><p>final Menu menu_1 = new Menu(newSubmenuMenuItem);<br />newSubmenuMenuItem.setMenu(menu_1);</p><p>final MenuItem newItemMenuItem = new MenuItem(menu_1, SWT.NONE);<br />newItemMenuItem.setText("退出");</p><p>final MenuItem newItemMenuItem_1 = new MenuItem(menu, SWT.NONE);<br />newItemMenuItem_1.setText("關於");</p><p>text = new Text(shell, SWT.BORDER);<br />final FormData fd_text = new FormData();<br />fd_text.right = new FormAttachment(100, -62);<br />fd_text.bottom = new FormAttachment(0, 25);<br />fd_text.top = new FormAttachment(0, 0);<br />fd_text.left = new FormAttachment(0, 0);<br />text.setLayoutData(fd_text);<br /> text.setText(browser.getUrl());</p><p>Button button;<br />button = new Button(shell, SWT.NONE);<br />final FormData fd_button = new FormData();<br />fd_button.right = new FormAttachment(100, -6);<br />fd_button.bottom = new FormAttachment(text, 22, SWT.TOP);<br />fd_button.top = new FormAttachment(text, 0, SWT.TOP);<br />button.setLayoutData(fd_button);<br />button.setText("轉到");<br />//<br />}</p><p>}<br />