| package com.ruifeng.swt.core;
import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell;
/** * 布局UTIL * * @author lpdx111 * */ public class LayoutUtil { /** * Shell 布局,居中 * * @param display * @param shell */ public static void centerShell(Display display, Shell shell) { Rectangle displayBounds = display.getPrimaryMonitor().getBounds(); Rectangle shellBounds = shell.getBounds(); int x = displayBounds.x + (displayBounds.width - shellBounds.width) >> 1; int y = displayBounds.y + (displayBounds.height - shellBounds.height) >> 1; shell.setLocation(x, y); }
public static void centerShell(Shell shell) { Rectangle displayBounds = shell.getDisplay().getPrimaryMonitor() .getBounds(); Rectangle shellBounds = shell.getBounds(); int x = displayBounds.x + (displayBounds.width - shellBounds.width) >> 1; int y = displayBounds.y + (displayBounds.height - shellBounds.height) >> 1; shell.setLocation(x, y); }
/** * jface dialog 根据窗口大小,获得居中的Point * * @param initialSize * @return */ public static Point getLocation(Point initialSize) { Rectangle displayBounds = Display.getCurrent().getPrimaryMonitor() .getBounds(); int x = displayBounds.x + (displayBounds.width - initialSize.x) >> 1; int y = displayBounds.y + (displayBounds.height - initialSize.y) >> 1; return new Point(x, y); }
}
摘自 lpdx111的专栏
| |