如果在JFrame那个类里Shell sl = new Shell();就会抛出 org.eclipse.swt.SWTException: Invalid thread access
机器上没有SWT的环境,另起一个线程试试。直接在事件派发线程中当然不行了
import java.awt.event.ActionEvent; import java.awt.event.ActionListener;import javax.swing.JButton; import javax.swing.JFrame;import org.eclipse.jface.dialogs.Dialog; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class Test extends JFrame{ public Test(){ JButton b = new javax.swing.JButton(); b.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { Display d = Display.getDefault(); Shell s = d.getActiveShell(); new Dialog(s){ @Override protected Control createDialogArea(Composite parent) { return super.createDialogArea(parent); }
}.open(); } });
getContentPane().add(b); }
public static void main(String[] args){ Test t = new Test(); t.show(); } } 家里没有SWT的环境,原以为在事件派发线程中可能会有问题,早晨看了一下事件没有问题
Shell sl = ds.getShell();
SampleDialog dialog = new SampleDialog(shell);
dialog.open();
org.eclipse.swt.SWTException: Invalid thread access
org.eclipse.swt.SWTException: Invalid thread access
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Test extends JFrame{ public Test(){
JButton b = new javax.swing.JButton();
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Display d = Display.getDefault();
Shell s = d.getActiveShell();
new Dialog(s){
@Override
protected Control createDialogArea(Composite parent) {
return super.createDialogArea(parent);
}
}.open();
}
});
getContentPane().add(b);
}
public static void main(String[] args){
Test t = new Test();
t.show();
}
}
家里没有SWT的环境,原以为在事件派发线程中可能会有问题,早晨看了一下事件没有问题