大夢想家

          5年開發(fā)工程師,2年實(shí)施經(jīng)理,X年售前顧問,......
          數(shù)據(jù)加載中……
          SWT編寫界面窗口時(shí)讓窗口處于屏幕中間

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

          直接調(diào)用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編寫界面窗口時(shí)讓窗口處于屏幕中間[未登錄] 2008-06-17 23:06 javaboy

          學(xué)習(xí)了 呵呵
            回復(fù)  更多評論    

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

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

          通常沒問題,因?yàn)閐isplayBounds.x=0.
            回復(fù)  更多評論    

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 赤峰市| 高安市| 文登市| 宜兰市| 辽中县| 齐齐哈尔市| 固安县| 伊宁市| 丰都县| 舞钢市| 大宁县| 中西区| 岗巴县| 鄱阳县| 托克逊县| 拉孜县| 永春县| 林周县| 昌乐县| 庆阳市| 青浦区| 孝感市| 阜康市| 济宁市| 甘洛县| 沙湾县| 容城县| 江阴市| 永春县| 简阳市| 万宁市| 文山县| 宁远县| 上蔡县| 兖州市| 西丰县| 壶关县| 武定县| 巴南区| 苍山县| 巴里|