如何从其他shell打开shell窗口关闭SWT中的第一个窗口?

时间:2014-12-29 07:58:11

标签: java swt

打开第二个窗口后,我想要处理第一个shell(登录)窗口..

如何关闭第一个窗口??

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

public class LoginForm1 
{
    static Text text = null;
    static Text text1= null;
    static Shell shell = null;

    public static void main (String [] args) 
    {
        Display display = new Display ();
        shell = new Shell(display);
        shell.setBounds(10, 10, 500, 1000);

        shell.setLayout(new GridLayout());
        shell.setText("Login form");

        Label label = new Label (shell, SWT.NONE);
        label.setText ("User Name");

        text = new Text (shell, SWT.BORDER);
        text.setTextLimit(5);

        Label label1 = new Label (shell, SWT.NONE);
        label1.setText ("Password");

        text1 = new Text (shell, SWT.BORDER);
        text1.setTextLimit(5);

              Button submit = new Button (shell, SWT.PUSH);
        submit.setText ("submit");

              submit.addListener(SWT.Selection, new Listener() 
    {
        public void handleEvent(Event event) 
        {
                          shell.addListener(SWT.Close, new Listener() 
                {
                    public void handleEvent(Event event) 
                    {
                        event.doit = false;
                    }
                });


                          final Shell shell1 = new Shell (shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
                shell1.setText("Dialog Shell");

                shell1.setLayout(new GridLayout());

                Button b= new Button(shell1, SWT.PUSH);
                b.setText("Define user interface");

                shell1.pack();
                shell1.open();
                }
            }
            }
        );

        shell.pack();
        shell.open();

        while (!shell.isDisposed()) 
        {
            if (!display.readAndDispatch())
            {
                display.sleep();
            }
        }
        display.dispose();
    }

}

以下部分不起作用..

shell.addListener(SWT.Close,new Listener()                     {                         public void handleEvent(事件事件)                         {                             event.doit = false;                         }                     });

这不起作用。 如何关闭第一个窗口...... ??

1 个答案:

答案 0 :(得分:1)

shell.close()

和你的代码

shell.addListener(SWT.Close, new Listener() 
                {
                    public void handleEvent(Event event) 
                    {
                        event.doit = false;
                    }
                });

阻止关闭shell,因此请将其删除。具有最小修复的代码应如下所示:

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class LoginForm 
{
    static Text text = null;
    static Text text1= null;
    static Shell shell = null;

    public static void main (String [] args) 
    {
        final Display display = new Display ();
        shell = new Shell(display);
        shell.setBounds(10, 10, 500, 1000);

        shell.setLayout(new GridLayout());
        shell.setText("Login form");

        Label label = new Label (shell, SWT.NONE);
        label.setText ("User Name");

        text = new Text (shell, SWT.BORDER);
        text.setTextLimit(5);

        Label label1 = new Label (shell, SWT.NONE);
        label1.setText ("Password");

        text1 = new Text (shell, SWT.BORDER);
        text1.setTextLimit(5);

              Button submit = new Button (shell, SWT.PUSH);
        submit.setText ("submit");

        submit.addListener(SWT.Selection, new Listener() 
        {
            public void handleEvent(Event event) 
            {
                /*
                shell.addListener(SWT.Close, new Listener() 
                {
                    public void handleEvent(Event event) 
                    {
                        event.doit = false;
                    }
                });
                */
                shell.close();


                shell = new Shell (display, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
                shell.setText("Dialog Shell");

                shell.setLayout(new GridLayout());

                Button b= new Button(shell, SWT.PUSH);
                b.setText("Define user interface");

                shell.pack();
                shell.open();
            }
        }
        );

        shell.pack();
        shell.open();

        while (!shell.isDisposed()) 
        {
            if (!display.readAndDispatch())
            {
                display.sleep();
            }
        }
        display.dispose();
    }

}