一、使用SWT本身
import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class LayoutUtil ...{ 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); } }
直接調用LayoutUtil.centerShell(Display display,Shell shell)即可使SWT窗口處于屏幕中央,其中,shell 要顯示的Shell對象。 二、借助AWT包里面獲取屏幕大小的方法
import java.awt.Toolkit; /** *//** * 在屏幕中間顯示Shell * @param shell 要顯示的Shell對象 */ private void centerShell(Shell shell) ...{ //得到屏幕的寬度和高度 int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height; int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width; //得到Shell窗口的寬度和高度 int shellHeight = shell.getBounds().height; int shellWidth = shell.getBounds().width; //如果窗口大小超過屏幕大小,讓窗口與屏幕等大 if(shellHeight > screenHeight) shellHeight = screenHeight; if(shellWidth > screenWidth) shellWidth = screenWidth; //讓窗口在屏幕中間顯示 shell.setLocation(( (screenWidth - shellWidth) / 2),((screenHeight - shellHeight) / 2) ); }
posted on 2007-12-25 14:21 阿南 閱讀(1607) 評論(2) 編輯 收藏