SWT 窗口居中

import org.eclipse.swt.widgets.*; protected static final Display r_swtDisplay = new Display(); protected static final Shell r_swtShell = new Shell(r_swtDisplay); /** * Center the window in screen */ protected void centerInScreen() { int nLocationX = Display.getCurrent().getClientArea().width / 2 - r_swtShell.getShell().getSize().x / 2; int nLocationY = Display.getCurrent().getClientArea().height / 2 - r_swtShell.getSize().y / 2; r_swtShell.setLocation(nLocationX, nLocationY); } /** * Center the window in parent */ protected void centerInParent() { Rectangle parentBounds = parentShell.getBounds(); Rectangle shellBounds = shell.getBounds(); shell.setLocation(parentBounds.x + (parentBounds.width - shellBounds.width) / 2, parentBounds.y + (parentBounds.height - shellBounds.height) / 2); }

你可能感兴趣的:(SWT 窗口居中)