SWT编写界面窗口时让窗口处于屏幕中间
一、使用SWT本身
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) );}