大夢想家

          5年開發(fā)工程師,2年實施經(jīng)理,X年售前顧問,......

          公告

          5年開發(fā)工程師,2年實施經(jīng)理,X年售前顧問,......
          <2007年12月>
          2526272829301
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          統(tǒng)計

          • 隨筆 - 130
          • 文章 - 5
          • 評論 - 427
          • 引用 - 0

          常用鏈接

          留言簿(23)

          我參與的團隊

          隨筆分類(188)

          隨筆檔案(123)

          文章分類(3)

          文章檔案(7)

          Friends

          Java Link

          My Link

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 262028
          • 排名 - 217

          最新評論

          閱讀排行榜

          評論排行榜

          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);
              }
          }

          直接調(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 阿南 閱讀(1609) 評論(2)  編輯  收藏

          評論

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

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

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

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

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

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 阿拉善盟| 宁南县| 马尔康县| 女性| 临潭县| 沧源| 波密县| 稻城县| 登封市| 宁晋县| 城固县| 松江区| 麻阳| 开化县| 克山县| 莆田市| 花垣县| 和政县| 阿图什市| 清徐县| 苗栗县| 忻州市| 桃源县| 奇台县| 淳安县| 香河县| 珲春市| 淮南市| 太湖县| 太谷县| 左权县| 玉林市| 内黄县| 泊头市| 册亨县| 锦州市| 新邵县| 凤翔县| 鲜城| 台安县| 定远县|