欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

swing显示swt页面,swingswt页面,java GUI两个对头

来源: javaer 分享于  点击 10006 次 点评:115

swing显示swt页面,swingswt页面,java GUI两个对头


java GUI两个对头swt,swing,swt是可以显示swing的页面,反过来呢,在swing中显示本地的swt页面其实也是可以做到的。

import java.awt.BorderLayout;import java.awt.Canvas;import java.awt.Dimension;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.SwingUtilities;import org.eclipse.swt.SWT;import org.eclipse.swt.awt.SWT_AWT;import org.eclipse.swt.layout.FillLayout;import org.eclipse.swt.widgets.Button;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Shell;/** * A simple canvas that encapsulates a SWT Browser instance. Add it to a AWT or * Swing container and call "connect()" after the container has been made * visible. */public class BrowserCanvas extends Canvas {    private Thread swtThread;    private Button swtBrowser;    /**     * Connect this canvas to a SWT shell with a Browser component and starts a     * background thread to handle SWT events. This method waits until the     * browser component is ready.     */    public void connect() {        if (this.swtThread == null) {            final Canvas canvas = this;            this.swtThread = new Thread() {                @Override                public void run() {                    try {                        Display display = new Display();                        Shell shell = SWT_AWT.new_Shell(display, canvas);                        shell.setLayout(new FillLayout());                        synchronized (this) {                            swtBrowser = new Button(shell, SWT.NONE);                            this.notifyAll();                        }                        shell.open();                        while (!isInterrupted() && !shell.isDisposed()) {                            if (!display.readAndDispatch()) {                                display.sleep();                            }                        }                        shell.dispose();                        display.dispose();                    } catch (Exception e) {                        interrupt();                    }                }            };            this.swtThread.start();        }        // Wait for the Browser instance to become ready        synchronized (this.swtThread) {            while (this.swtBrowser == null) {                try {                    this.swtThread.wait(100);                } catch (InterruptedException e) {                    this.swtBrowser = null;                    this.swtThread = null;                    break;                }            }        }    }    /**     * Returns the Browser instance. Will return "null" before "connect()" or     * after "disconnect()" has been called.     */    public Button getBrowser() {        return this.swtBrowser;    }    /**     * Stops the swt background thread.     */    public void disconnect() {        if (swtThread != null) {            swtBrowser = null;            swtThread.interrupt();            swtThread = null;        }    }    /**     * Ensures that the SWT background thread is stopped if this canvas is     * removed from it's parent component (e.g. because the frame has been     * disposed).     */    @Override    public void removeNotify() {        super.removeNotify();        disconnect();    }    /**     * Opens a new JFrame with BrowserCanvas in it     */    public static void main(String[] args) {        // Required for Linux systems        System.setProperty("sun.awt.xembedserver", "true");        // Create container canvas. Note that the browser        // widget will not be created, yet.        final BrowserCanvas browserCanvas = new BrowserCanvas();        browserCanvas.setPreferredSize(new Dimension(800, 600));        JPanel panel = new JPanel(new BorderLayout());        panel.add(browserCanvas, BorderLayout.CENTER);        // Add container to Frame        final JFrame frame = new JFrame("My SWT Browser");        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);        frame.setContentPane(panel);        frame.pack();        // This is VERY important: Make the frame visible BEFORE        // connecting the SWT Shell and starting the event loop!        frame.setVisible(true);        browserCanvas.connect();        // Now we can open a webpage, but remember that we have        // to use the SWT thread for this.        browserCanvas.getBrowser().getDisplay().asyncExec(new Runnable() {            @Override            public void run() {                browserCanvas.getBrowser().setText("aaaaaa");            }        });    }}上面代码还有一点缺陷就是需要拉动一下窗体里面的swt才会出现,这个可以通过代码触发事件完成,最少swt和swing共存了。//该片段来自于http://byrx.net
相关栏目:

用户点评