大夢想家

          5年開發工程師,2年實施經理,X年售前顧問,......
          數據加載中……
          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) );
          }



          客戶虐我千百遍,我待客戶如初戀!

          posted on 2007-12-25 14:21 阿南 閱讀(1608) 評論(2)  編輯  收藏

          評論

          # re: SWT編寫界面窗口時讓窗口處于屏幕中間[未登錄] 2008-06-17 23:06 javaboy

          學習了 呵呵
            回復  更多評論    

          # re: SWT編寫界面窗口時讓窗口處于屏幕中間[未登錄] 2008-08-05 11:42 andrew

          int x = displayBounds.x + (displayBounds.width - shellBounds.width)>>1;
          這一行某些時候有錯誤。
          應該改為:
          int x = displayBounds.x +( (displayBounds.width - shellBounds.width)>>1);

          通常沒問題,因為displayBounds.x=0.
            回復  更多評論    

          只有注冊用戶登錄后才能發表評論。


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 洛隆县| 长汀县| 名山县| 龙里县| 汶上县| 兰州市| 肇州县| 黎川县| 西青区| 清原| 资源县| 镇平县| 常宁市| 垦利县| 芜湖市| 镇宁| 黄平县| 双辽市| 白沙| 临桂县| 青铜峡市| 南城县| 竹北市| 喀喇沁旗| 同江市| 商丘市| 镇沅| 五常市| 厦门市| 象州县| 疏附县| 车险| 肥乡县| 蕉岭县| 兴宁市| 武平县| 丰镇市| 凤山市| 泰安市| 当涂县| 枣强县|