当前位置: 代码迷 >> Java相关 >> Swing界面点击监听打开swt界面卡死有关问题
  详细解决方案

Swing界面点击监听打开swt界面卡死有关问题

热度:6541   发布时间:2013-02-25 21:48:14.0
Swing界面点击监听打开swt界面卡死问题
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.ole.win32.OleClientSite;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test {

private static OleFrame oleFrame1;
private static OleClientSite clientSite;

public static void main(String[] args) {

final JFrame frame = new JFrame();
// 关键 使用 canvas 进行交互
final JButton jb = new JButton("按一下");
frame.add(jb);
frame.setVisible(true);
frame.setSize(1024, 780);
jb.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
Canvas canvas = new Canvas();
frame.remove(jb);
JPanel jp = new JPanel();
frame.getContentPane().add(jp);
jp.add(canvas, BorderLayout.CENTER);
jp.getParent().add(canvas, BorderLayout.CENTER);
final Display display = new Display();
// 利用SWT_AWT.new_Shell 交互
final Shell shell = SWT_AWT.new_Shell(display, canvas);
shell.setLayout(new FillLayout());
shell.layout();
shell.setSize(1024, 780);
initGUI(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
});
}
private static void initGUI(Shell shell) {
oleFrame1 = new OleFrame(shell, SWT.NONE);
try {
clientSite = new OleClientSite(oleFrame1, SWT.NULL, new File(
"F:/image/abc.doc"));
System.out.println(clientSite.getProgramID() + ", " + clientSite);
clientSite.doVerb(OLE.OLEIVERB_SHOW);

} catch (org.eclipse.swt.SWTException e) {
String str = "Create OleClientSite Error" + e.toString();
System.out.println(str);
return;
}
shell.layout();
}
}


如果没有按钮监听事件,则能够直接打开,现在有了,貌似是swt线程的问题,小弟不是很明白,
求大侠们指点下 ,灰常感谢

------解决方案--------------------------------------------------------
main方法要这样写:
Java code
    public static void main(String[] args) {        final Display display = Display.getDefault();        final JFrame frame = new JFrame();        // 关键 使用 canvas 进行交互        final JButton jb = new JButton("按一下");        frame.add(jb);        frame.setVisible(true);        frame.setSize(1024, 780);        jb.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                final Canvas canvas = new Canvas();                frame.remove(jb);                JPanel jp = new JPanel();                frame.getContentPane().add(jp);                jp.add(canvas, BorderLayout.CENTER);                jp.getParent().add(canvas, BorderLayout.CENTER);                // 利用SWT_AWT.new_Shell 交互                display.syncExec(new Runnable(){                        public void run(){                            final Shell shell = SWT_AWT.new_Shell(display, canvas);                        shell.setLayout(new FillLayout());                        shell.layout();                        shell.setSize(1024, 780);                        initGUI(shell);                        shell.open();                        shell.setVisible(true);                       }                    });             }        });        while (frame.isDisplayable())            if (!display.readAndDispatch())                display.sleep();    }