SWT第一个窗口触发事件后弹出一个新的窗口,怎么样能够弹出新窗口后,自动关闭旧的窗口,谢谢大家给个例子。

解决方案 »

  1.   

    import org.eclipse.jface.dialogs.Dialog;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.widgets.Button;
    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 TestDialog extends Dialog{
    private Button mButton = null;

    public TestDialog(Shell shell){
    super(shell);
    }


    protected Control createDialogArea(Composite parent) { Composite result = (Composite) super.createDialogArea(parent);
    mButton = new Button(result, SWT.PUSH);
    mButton.setText("Click");
    mButton.addSelectionListener(new SelectionAdapter() {
    public void widgetSelected(SelectionEvent e) {
    cancelPressed(); 先关闭父窗口
    (new NewDialog(getParentShell())).open(); 弹出新窗口
    }
    });
    return result;
    }

    protected void cancelPressed() {
    super.cancelPressed();
    }

    class NewDialog extends Dialog{
    public NewDialog(Shell shell){
    super(shell);
    }
    } public static void main(String args[]){
    Display display = new Display();
    Shell shell= new Shell(display);
    shell.setText("Test");
    shell.open();

    TestDialog dialog = new TestDialog(shell);
    dialog.open();
    while(!shell.isDisposed()){
    if(!display.readAndDispatch()){
    display.sleep();
    }
    }
    display.dispose();
    }
    }
      

  2.   

    可以用  dispose() 把旧窗口关掉
    后面再接要调用的新窗口就好了 
      

  3.   


    import java.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;public public class main extends JFrame{
       JButton jb = new JButton("新窗口");
       
       JPanel jp = new JPanel();   private void JBInit(){
            jp.add(jb);
            this.add("Center", jp);
            this.setSize(400, 130);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       }   public main() {
            try {
                addListener();
                JBInit();
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }   public static void main(String[] args) {
            main m = new main();
            m.setVisible(true);
       }   private void addListener(){
            jb.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    showFrame();
                }
            });
       }   private void showFrame(){
            this.setVisible(false);
            newFrame nf = new newFrame();
            nf.setVisible(true);
       }
    }//新窗体
    import java.swing.*;public public class newFrame extends JFrame{   private void JBInit(){
            this.setSize(400, 130);
            this.setResizable(false);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       }   public newFrame() {
            JBInit();
        }
    }