我现在写了一个shell的窗体,在加载显示时要同时加载另一个Shell的小窗体显现出来,上面有句等待提示。当两个窗体同时显现时提示窗体要焦点独占,大窗体上的控件都点不动。
只有当过了几十秒,小窗体自动消失后,大窗体才能获得焦点。请问如何做哦?看SWT中好像没有独占焦点的办法哦?请教高人指点一下。

解决方案 »

  1.   

    你使用的是Dialog shell。运行下面的示例代码:import org.eclipse.swt.SWT;
    import org.eclipse.swt.layout.FillLayout;
    import org.eclipse.swt.layout.RowLayout;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Label;
    import org.eclipse.swt.widgets.Shell;public class Y { protected Shell shell;
    private Thread daemonThread; /**
     * Launch the application
     * 
     * @param args
     */
    public static void main(String[] args) {
    try {
    Y window = new Y();
    window.open();
    } catch (Exception e) {
    e.printStackTrace();
    }
    } /**
     * Open the window
     */
    public void open() {
    final Display display = Display.getDefault();
    createContents();
    shell.open();
    shell.layout();
    daemonThread = new Thread() {
    private Shell myshell; @Override
    public void run() {
    // 等待5秒钟窗口出来
    try {
    Thread.sleep(5000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    createWindows();
    swtichFocus();
    destroyWindows();
    } private void destroyWindows() {
    display.asyncExec(new Runnable() {
    @Override
    public void run() {
    myshell.dispose();
    }
    }); } private void swtichFocus() {
    try {
    Thread.sleep(5000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    } private void createWindows() {
    display.asyncExec(new Runnable() { @Override
    public void run() {
    while (myshell == null) {
    myshell = new Shell(shell);
    myshell.setText("Foo");
    myshell.setSize(300, 300);
    Label l = new Label(myshell,SWT.CENTER);
    myshell.setLayout(new RowLayout());
    l.setText("I live in short time!");
    myshell.open();
    }
    }
    }); }
    };
    daemonThread.start();
    while (!shell.isDisposed()) {
    if (!display.readAndDispatch())
    display.sleep();
    }
    } /**
     * Create contents of the window
     */
    protected void createContents() {
    shell = new Shell();
    shell.setSize(500, 375);
    shell.setText("SWT Application");
    shell.setLayout(new FillLayout());
    Label l = new Label(shell, SWT.CENTER);
    l.setText("Waitting in five seconds"); }}