1

我正在使用 SWT JFace 对话框。

我向 OK 按钮添加了一个侦听器,我想在用户单击 OK 按钮后显示一个消息框。这一步的问题是,当我单击 OK 按钮时,shell 会被处理掉。我怎样才能防止这种行为?

4

1 回答 1

2

以下代码将防止对话框通过“确定”按钮关闭。只是不要调用方法this.close()okPressed()

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    new OptionsDialog(shell).open();

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

private static class OptionsDialog extends Dialog {

    private Composite composite;

    public OptionsDialog(Shell parentShell)
    {
        super(parentShell);
        setShellStyle(parentShell.getStyle() | SWT.CLOSE | SWT.TITLE | SWT.BORDER | SWT.APPLICATION_MODAL);
        setBlockOnOpen(true);
    }

    protected Control createDialogArea(Composite parent) {
        this.composite = (Composite) super.createDialogArea(parent);

        GridLayout layout = new GridLayout(1, false);
        layout.marginHeight = 5;
        layout.marginWidth = 10;

        composite.setLayout(layout);

        createContent();

        return composite;
    }

    private void createContent()
    {
        /* add your widgets */
    }

    protected void configureShell(Shell newShell)
    {
        super.configureShell(newShell);
        newShell.setText("Shell name");
    }

    public void okPressed()
    {
        /* DO NOTHING HERE!!! */
        //this.close();
    }
}
于 2012-09-09T08:57:05.677 回答