实践-全程

预测未来的最好办法,就是把它创造出来 --- 阿伦.凯
数据加载中……
SWT编写界面窗口时让窗口处于屏幕中间

一、使用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) );
}



声明:  
  本BLOG内的所有文章,未经特别说明,均采用“署名-非商业性使用-禁止演绎 2.5 中国大陆”授权。任何违反本协议的行为均属于非法行为。如需非商业性转载,请保留署名。如需商业性转载出版,请直接和我联系。

Creative Commons License

posted on 2007-12-25 14:21 阿南 阅读(237) 评论(1)  编辑  收藏

评论

# re: SWT编写界面窗口时让窗口处于屏幕中间[未登录] 2008-06-17 23:06 javaboy

学习了 呵呵
  回复  更多评论    



标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交