SWT编写界面窗口时让窗口处于屏幕中间

一、使用SWT本身

Java代码 复制代码  收藏代码
  1. import org.eclipse.swt.graphics.Rectangle;   
  2. import org.eclipse.swt.widgets.Display;   
  3. import org.eclipse.swt.widgets.Shell;   
  4.   
  5. public class LayoutUtil ...{   
  6.   
  7.     public static void centerShell(Display display,Shell shell)...{   
  8.         Rectangle displayBounds = display.getPrimaryMonitor().getBounds();   
  9.         Rectangle shellBounds = shell.getBounds();   
  10.         int x = displayBounds.x + (displayBounds.width - shellBounds.width)>>1;   
  11.         int y = displayBounds.y + (displayBounds.height - shellBounds.height)>>1;   
  12.         shell.setLocation(x, y);   
  13.     }   
  14. }  
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包里面获取屏幕大小的方法

Java代码 复制代码  收藏代码
  1. import java.awt.Toolkit;   
  2. /** *//**  
  3. * 在屏幕中间显示Shell  
  4. * @param shell 要显示的Shell对象  
  5. */  
  6. private void centerShell(Shell shell)   
  7. {   
  8.          //得到屏幕的宽度和高度   
  9.          int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;   
  10.          int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;   
  11.          //得到Shell窗口的宽度和高度   
  12.          int shellHeight = shell.getBounds().height;   
  13.          int shellWidth = shell.getBounds().width;   
  14.          //如果窗口大小超过屏幕大小,让窗口与屏幕等大   
  15.          if(shellHeight > screenHeight)   
  16.                    shellHeight = screenHeight;   
  17.          if(shellWidth > screenWidth)   
  18.                   shellWidth = screenWidth;   
  19.         //让窗口在屏幕中间显示   
  20.         shell.setLocation(( (screenWidth - shellWidth) / 2),((screenHeight - shellHeight) / 2) );   
  21. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值