我有一个swt的dialog
LoginDialog loginDialog = new LoginDialog(shell);
loginDialog.open();
怎么让他打开后显示在屏幕中央?好像没有类似setLocation的方法

解决方案 »

  1.   

    你要自己继承Dialog,在open()里设置居中
    public class LoginDialog extends Dialog {
    Object result; public LoginDialog(Shell parent, int style) {
    super(parent, style);
    } public LoginDialog(Shell parent) {
    this(parent, 0);
    } public Object open() {
    Shell parent = getParent();
    Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
    shell.setText(getText());
    shell.setLocation(x, y);// setLocation here
    shell.open();
    Display display = parent.getDisplay();
    while (!shell.isDisposed()) {
    if (!display.readAndDispatch())
    display.sleep();
    }
    return result;
    }
    }
      

  2.   

    loginDialog.setLocationRelativeTo(null);   
      

  3.   

    open()之前做以下操作看看! Shell parent = (Shell) loginDialog.getParent().getParent();
    Shell shell = loginDialog.getParent();// this is the shell that is Dialog's Parent

    Rectangle rectParent = parent.getBounds();
    Rectangle rectShell = shell.getBounds();
    shell.setBounds(
    rectParent.x + (rectParent.width - rectShell.width) / 2,
    rectParent.y + (rectParent.height - rectShell.height) / 2,
    rectShell.width,
    rectShell.height);