2012-09-09 50 views

回答

2

以下代碼將通過「確定」按鈕阻止對話框關閉。只是不要在okPressed()方法調用this.close()

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(); 
    } 
} 
+0

感謝很多:)其工作 – user1205079

+0

@ user1205079大,有樂趣。 – Baz

相關問題