大夢想家

          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.
            回復  更多評論    

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


          網站導航:
           
          主站蜘蛛池模板: 周宁县| 新丰县| 宜都市| 崇左市| 寿光市| 庆云县| 大荔县| 海门市| 香格里拉县| 淮南市| 武隆县| 珠海市| 都江堰市| 晋城| 福州市| 青川县| 上杭县| 延寿县| 台北县| 恭城| 澄城县| 威远县| 梨树县| 青海省| 巴塘县| 海门市| 拉萨市| 东乌珠穆沁旗| 大洼县| 威海市| 娱乐| 邓州市| 雷山县| 澎湖县| 怀宁县| 庆元县| 郎溪县| 阜南县| 明溪县| 鹰潭市| 运城市|