iteye_6843 2008-08-25 17:36
浏览 213
已采纳

SWT中,如何使得关闭主窗体的同时关闭其他附属窗体?

如题,具体点说,也就是,我打开主窗体后,开了其他附属窗体,现在我想直接关闭主窗体,请问,如何使得能在关闭主窗体的同时,自动关闭这些附属窗体?程序应该如何实现?谢谢!

  • 写回答

3条回答 默认 最新

  • lggegegmail 2008-08-26 09:43
    关注

    使用父窗体的shell 作为 子窗体的parent shell, 在你关闭父窗体时, 会自动关闭从这个父窗台弹出的所有子窗体.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • lggegegmail 2008-08-26 09:41
    关注

    主窗体的代码:
    [code="java"]public class SwtDialog extends Dialog {

    protected Object result;
    protected Shell shell;
    
    /**
     * Create the dialog
     * @param parent
     */
    public SwtDialog(Shell parent) {
        super(parent, SWT.NONE);
    }
    
    /**
     * Open the dialog
     * @return the result
     */
    public Object open() {
        createContents();
        shell.open();
        shell.layout();
        Display display = getParent().getDisplay();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        return result;
    }
    /**
     * Create contents of the dialog
     */
    protected void createContents() {
        shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
        shell.setSize(500, 375);
        shell.setText("SWT Dialog");
    
        final Button button = new Button(shell, SWT.NONE);
        button.setText("button");
        button.setBounds(32, 31, 44, 23);
    
        button.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                new PopUpDialog(shell).open(); 
    

    // 注意这里,使用当前窗口的shell作为子窗台的parent 的shell.
    }
    });
    }
    }[/code]

    评论
  • lggegegmail 2008-08-26 09:42
    关注

    子窗台的代码[一般代码]
    [code="java"]public class PopUpDialog extends Dialog {
    protected Object result;
    protected Shell shell;

    public PopUpDialog(Shell parent) {
        super(parent, SWT.NONE);
    }
    
    public Object open() {
        createContents();
        shell.open();
        shell.layout();
        Display display = getParent().getDisplay();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        return result;
    }
    
    protected void createContents() {
        shell = new Shell(getParent(), SWT.DIALOG_TRIM /* | SWT.APPLICATION_MODAL */ );
        shell.setSize(500, 375);
        shell.setText("SWT Dialog");
    }
    

    }[/code]

    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 webapi的部署(标签-服务器)
  • ¥20 怎么加快手机软件内部计时的时间(关键词-日期时间)
  • ¥15 C语言除0问题的检测方法
  • ¥15 为什么四分管的内径有的是16mm有的15mm,四分不应该是12.7mm吗
  • ¥15 macos13下 ios交叉编译的问题
  • ¥15 bgz压缩文件怎么打开
  • ¥15 封装dll(引入了pcl的点云设计库)
  • ¥30 关于#开发语言#的问题:我需要在抄板的基础上再抄板抄程序,根据RDA8851CM基础上开发
  • ¥15 oracle 多个括号,怎么删除指定的括号及里面的内容?
  • ¥15 小新14API2019想用bios调风扇