SWT可互動式Browser控制項

來源:互聯網
上載者:User

在許多平台中,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 />

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.