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