/**
* 設置窗口位於屏幕中間
* @param shell 要調整位置的窗口對象
*/
public static void center(Shell shell)
{
//獲取屏幕高度和寬度
int screenH = Toolkit.getDefaultToolkit().getScreenSize().height;
int screenW = Toolkit.getDefaultToolkit().getScreenSize().width;
//獲取對象窗口高度和寬度
int shellH = shell.getBounds().height;
int shellW = shell.getBounds().width;
//如果對象窗口高度超出屏幕高度,則強制其與屏幕等高
if(shellH > screenH)
shellH = screenH;
//如果對象窗口寬度超出屏幕寬度,則強制其與屏幕等寬
if(shellW > screenW)
shellW = screenW;
//定位對象窗口坐標
shell.setLocation(((screenW - shellW) / 2), ((screenH - shellH) / 2));
}
/**
* 設置窗口位於屏幕中間
* @param display 設備
* @param shell 要調整位置的窗口對象
*/
public static void center(Display display, Shell shell)
{
Rectangle bounds = display.getPrimaryMonitor().getBounds();
Rectangle rect = shell.getBounds();
int x = bounds.x + (bounds.width - rect.width) / 2;
int y = bounds.y + (bounds.height - rect.height) / 2;
shell.setLocation(x, y);
}
