隨筆-208  評論-469  文章-30  trackbacks-0
          相信大家都使用過MSN,QQ這樣的即時(shí)聊天類軟件,對于它們的好友上線提示功能并不陌生吧?從屏幕右下角彈出一個(gè)小界面,慢慢上升,最后消失。我們能不能在自已的程序中也做出相同的功能呢?能!筆者現(xiàn)用JAVA和eclipse的SWT用戶界面組件實(shí)現(xiàn)這個(gè)功能。

            什么是SWT呢?

            SWT原來是eclipse項(xiàng)目組為開發(fā)eclipse IDE所編寫的圖形界面API,運(yùn)行時(shí),其先判斷本機(jī)是否有相同的界面元素,如果有則直接調(diào)用顯示,如沒有才進(jìn)行模擬顯示。其運(yùn)行機(jī)制使速度比AWT,SWING快很多。

            了解更多請看:http://www.eclipse.org/swt

            編寫思路

            先取得用戶屏幕大小,用屏幕高度減去popup界面的高度計(jì)算出popup界面在屏幕顯示的最高位置(當(dāng)界面移動(dòng)到此位置時(shí)就停止移動(dòng))。

          Rectangle area = Display.getDefault().getClientArea();
          int upPosition = area.height - 100;

            用屏幕高度加上popup界面的高度就計(jì)算出popup界面的初始位置(初始化時(shí)不可見,然后慢慢上移到upPosition點(diǎn)后停止移動(dòng),再顯示若干秒后消失)。

          int downPosition = area.height + 100;

            移動(dòng)位置我們用線程實(shí)現(xiàn),當(dāng)初始化界面后,調(diào)用start()方法運(yùn)行此線程,在線程中循環(huán)判斷downPosition的大小是否小于upPosition,如果小于的話說明還未到停止的時(shí)候,設(shè)置popup界面的邊框?yàn)閐ownPosition,并暫停10毫秒,如果downPosition大于upPosition的,說明popup界面已移動(dòng)到了最高位置。調(diào)用sleep()暫停5秒鐘后關(guān)閉界面并退出程序。就這么簡單,ok, Let's go! 下面給出整個(gè)程序代碼:

          這是運(yùn)行后的效果o_img1.gif?
          在這之前要像我下面一樣把swt包導(dǎo)進(jìn)來!
          ?{DA72CB76-6E12-4B99-9C8A-6C0A9D09EFAD}.JPG
          //?Test.java

          import?org.eclipse.swt.SWT;
          import?org.eclipse.swt.events.SelectionAdapter;
          import?org.eclipse.swt.events.SelectionEvent;
          import?org.eclipse.swt.widgets.Button;
          import?org.eclipse.swt.widgets.Display;
          import?org.eclipse.swt.widgets.Shell;

          public?class?Test?{

          ?
          public?static?void?main(String[]?args)?{
          ??
          ??final?Display?display?
          =?new?Display();
          ??Shell?shell?
          =?new?Shell();
          ??shell.setText(
          "aaa");
          ??shell.setSize(
          250,?150);
          ??
          ??final?Button?button?
          =?new?Button(shell,?SWT.NONE);
          ??button.setBounds(
          50,?20,?100,?25);
          ??button.setText(
          "button");??
          ??button.addSelectionListener(
          new?SelectionAdapter()?{
          ???
          public?void?widgetSelected(SelectionEvent?e)?{
          ????System.
          out.println("click");
          ????Popup?popup?
          =?new?Popup("您的好友xxx上線了。");
          ????popup.start();
          ???}

          ??}
          );
          ??
          ??shell.open();
          ????
          ??
          while?(!shell.isDisposed())?{
          ???
          if?(!display.readAndDispatch())?{
          ????display.sleep();
          ???}

          ??}

          ??display.dispose();
          ?}

          }



          //Popup.java

          import?org.eclipse.swt.SWT;
          import?org.eclipse.swt.graphics.Rectangle;
          import?org.eclipse.swt.widgets.Display;
          import?org.eclipse.swt.widgets.Shell;
          import?org.eclipse.swt.widgets.Text;

          public?class?Popup?extends?Thread?{

          ?Shell?shell;

          ?
          protected?int?moveStep?=?2;
          ?
          protected?int?upPosition;
          ?
          protected?int?downPosition;
          ?
          protected?int?leftPosition;

          ?
          public?Popup(final?String?message)?{

          ??shell?
          =?new?Shell(SWT.ON_TOP);
          ??Text?text?
          =?new?Text(shell,?SWT.MULTI?|?SWT.WRAP);
          ??text.setBounds(
          10,?20,?180,?80);
          ??text.setBackground(shell.getBackground());??
          ??text.setText(message);
          ??Rectangle?area?
          =?Display.getDefault().getClientArea();

          ??upPosition?
          =?area.height?-?100;
          ??downPosition?
          =?area.height?+?100;
          ??leftPosition?
          =?area.width?-?180;

          ??shell.setSize(
          180,?100);
          ??shell.setLocation(leftPosition,?downPosition);

          ??shell.open();

          ?}


          ?
          public?void?run()?{

          ??Display?display?
          =?shell.getDisplay();
          ??
          while?(true)?{
          ???
          try?{
          ????Thread.sleep(
          10);
          ????
          if?((downPosition?-?moveStep)?>?upPosition)?{
          ?????display.asyncExec(
          new?Runnable()?{
          ??????
          public?void?run()?{
          ???????shell.setLocation(
          ????????leftPosition,
          ????????downPosition?
          -?moveStep);
          ???????downPosition?
          -=?moveStep;
          ??????}

          ?????}
          );
          ????}
          ?else?{
          ?????Thread.sleep(
          5000);
          ?????display.asyncExec(
          new?Runnable()?{
          ??????
          public?void?run()?{
          ???????shell.dispose();
          ??????}

          ?????}
          );
          ????}

          ???}
          ?catch?(InterruptedException?e)?{
          ????e.printStackTrace();
          ???}

          ??}

          ?}

          }

          posted on 2007-02-05 23:05 EricWong 閱讀(501) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 历史| 武冈市| 饶河县| 武山县| 双桥区| 家居| 穆棱市| 内黄县| 廉江市| 巧家县| 贡嘎县| 长子县| 调兵山市| 沈阳市| 滨海县| 蒙阴县| 保靖县| 珠海市| 义乌市| 固阳县| 白水县| 安阳市| 南皮县| 广河县| 许昌县| 新乐市| 自贡市| 凉城县| 邵阳县| 陕西省| 闽清县| 印江| 昌乐县| 西充县| 巧家县| 星座| 永吉县| 长葛市| 栖霞市| 雷州市| 孟津县|