首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

分享下如何把swt嵌入到swing中,另外再问点有关问题

2012-01-21 
分享下怎么把swt嵌入到swing中,另外再问点问题先上代码Java codeimport java.awt.BorderLayoutimport jav

分享下怎么把swt嵌入到swing中,另外再问点问题
先上代码

Java code
import java.awt.BorderLayout;import java.awt.Canvas;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.SwingUtilities;import org.eclipse.swt.SWT;import org.eclipse.swt.awt.SWT_AWT;import org.eclipse.swt.browser.Browser;import org.eclipse.swt.browser.OpenWindowListener;import org.eclipse.swt.browser.ProgressEvent;import org.eclipse.swt.browser.ProgressListener;import org.eclipse.swt.browser.WindowEvent;import org.eclipse.swt.layout.FillLayout;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Shell;public class Window extends JFrame {    private static final long serialVersionUID = 1L;    private JPanel panel;    private JButton b1 = new JButton("GO"),                    b2 = new JButton("Add Data");    private JTextField textField = new JTextField(50);    static String path = "http://www.baidu.com";    static final Display display = Display.getDefault();    static Canvas canvas = null;    static Shell shell;    static Browser browser = null;        public Window() {        System.setProperty("sun.awt.xembedserver", "true");        canvas = new Canvas() {            private static final long serialVersionUID = 1L;            @Override            public void addNotify() {                super.addNotify();                final Canvas canvas_ = this;                display.asyncExec(new Runnable() {                    public void run() {                        shell = SWT_AWT.new_Shell(display, canvas);                        shell.setText("Slashdot");                        shell.setLayout(new FillLayout());                        browser = new Browser(shell, SWT.NONE);                        browser.addProgressListener(new ProgressListener() {                            public void changed(ProgressEvent event) {                            }                            public void completed(ProgressEvent event) {                                /*String script = "var a = new Array();" +                                                "a = document.getElementsByTagName(\"a\");" +                                                "for(i = 0; i < a.length; i++) {" +                                                "a[i].target = '_self';" +                                                "}";                                browser.execute(script);*/                                System.out.println("网页加载完成!");                            }                        });                        browser.addOpenWindowListener(new OpenWindowListener() {                            @Override                            public void open(WindowEvent event) {                                final Browser browser2 = new Browser(shell, SWT.NONE);                                event.browser = browser2;                                event.display.asyncExec(new Runnable() {                                    @Override                                    public void run() {                                        browser.setUrl(browser2.getUrl());                                    }                                                                    });                            }                                                    });                        browser.setUrl(path);                        browser.setVisible(true);                        shell.setSize(canvas_.getWidth(), canvas_.getHeight());                        shell.open();                        while(!shell.isDisposed()){                            if(!display.readAndDispatch()){                                display.sleep();                            }                        }                    }                });            }        };        panel = new JPanel();        panel.setLayout(new FlowLayout());        panel.add(textField);        b1.addActionListener(new ActionListener() {            @Override            public void actionPerformed(ActionEvent e) {                path = textField.getText();                display.asyncExec(new Runnable() {                    public void run() {                        browser.setUrl(path);                    }                });            }                    });        panel.add(b1);        add(BorderLayout.NORTH, panel);        add(BorderLayout.CENTER, canvas);        setSize(800, 600);        ImageIcon ii = new ImageIcon(this.getClass().getResource("image/debug_wiz.png"));         setIconImage(ii.getImage());        setTitle("swt嵌入swing");        setBounds(200, 200, 800, 600);        setVisible(true);    }        static Window window;        public static void main(String[] args) {        SwingUtilities.invokeLater(new Runnable() {            public void run() {                window = new Window();            }        });        while(true){            if(!display.readAndDispatch()){                display.sleep();            }        }    }} 



问题就是,我这么做在关闭窗口的时候,窗口虽然关闭了,但是这个类还在运行,具体可以运行下试试。

我要怎么做才能正确的关闭呢。

[解决办法]
你在Window()构造方法的最后添加一行
Java code
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

热点排行