java swing 內建瀏覽器開啟網頁顯示flash圖表-swt Browser應用

來源:互聯網
上載者:User

今天在網上找了好久如何用在java swing開啟網頁,從而實現顯示網頁圖表的效果,功夫不負有心人,終於搞定了,下面把所用的類和swt.jar整理了一下,方便有需要的朋友使用。

用到的swt.jar下載

調用網頁的Browser要結合現有的java控制項使用,一下是結合panel定義的類(SWTPane.java):

 

 /*   * To change this template, choose Tools | Templates   * and open the template in the editor.   */   package desktopapplicationmenu.comm;      import java.awt.BorderLayout;   import java.awt.Canvas;   import java.awt.Panel;   import org.eclipse.swt.SWT;   import org.eclipse.swt.awt.SWT_AWT;   import org.eclipse.swt.browser.Browser;   import org.eclipse.swt.layout.FillLayout;      import org.eclipse.swt.widgets.Display;   import org.eclipse.swt.widgets.Shell;      /**   *   * @author liujl   */   public class SWTPane extends Panel {          DisplayThread displayThread;       private Canvas canvas;          public SWTPane() {           displayThread = new DisplayThread();           displayThread.start();           canvas = new Canvas();           setLayout(new BorderLayout());           add(canvas, BorderLayout.CENTER);       }    public static void main(String args[]) throws Exception {           java.awt.EventQueue.invokeLater(new Runnable() {                  public void run() {                   new SWTPane().setVisible(true);               }           });            }       public void addNotify() {           super.addNotify();           Display dis = displayThread.getDisplay();           dis.syncExec(new Runnable() {                  public void run() {                   Shell shell = SWT_AWT.new_Shell(displayThread.getDisplay(), canvas);                   shell.setLayout(new FillLayout());                   final Browser browser = new Browser(shell, SWT.NONE);                   browser.setLayoutData(BorderLayout.CENTER);                   browser.setUrl("http://www.my400800.cn");               }           });       }   }  
 

裡面為了防止在開啟網頁的時候出現錯誤,封裝了一下Thread類,定義如下(DisplayThread.java):

 

/* * To change this template, choose Tools | Templates * and open the template in the editor. */package desktopapplicationmenu.comm;import org.eclipse.swt.widgets.Display;/** * * @author liul */public class DisplayThread extends Thread {    private Display display;    Object sem = new Object();    public void run() {        synchronized (sem) {            display = Display.getDefault();            sem.notifyAll();        }        swtEventLoop();    }    private void swtEventLoop() {        while (true) {            if (!display.readAndDispatch()) {                display.sleep();            }        }    }    public Display getDisplay() {        try {            synchronized (sem) {                while (display == null) {                    sem.wait();                }                return display;            }        } catch (Exception e) {            return null;        }    }}

調用方法

        SWTPane jbtn_Sel = new SWTPane();
        jPanel1.add(jbtn_Sel);
        jbtn_Sel.setBounds(1, 1, 600, 600);

相關文章

聯繫我們

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