大夢想家

          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 阿南 閱讀(1610) 評論(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.
            回復  更多評論    

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


          網站導航:
           
          主站蜘蛛池模板: 瓦房店市| 阿拉善盟| 防城港市| 武宁县| 修文县| 信阳市| 平潭县| 青海省| 陵川县| 正宁县| 诏安县| 清水县| 灵璧县| 柞水县| 鹤岗市| 兰溪市| 昌黎县| 盐源县| 醴陵市| 万盛区| 天全县| 枝江市| 玛曲县| 涟源市| 德安县| 萍乡市| 滨州市| 齐齐哈尔市| 甘洛县| 河池市| 临邑县| 离岛区| 同仁县| 彰武县| 马关县| 贵南县| 榆社县| 砀山县| 思茅市| 麻阳| 栖霞市|