春風博客

          春天里,百花香...

          導航

          <2007年9月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          統計

          公告

          MAIL: junglesong@gmail.com
          MSN: junglesong_5@hotmail.com

          Locations of visitors to this page

          常用鏈接

          留言簿(11)

          隨筆分類(224)

          隨筆檔案(126)

          個人軟件下載

          我的其它博客

          我的鄰居們

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          如何讓Swing控件如JLabel,JButton等顯示動態Gif圖片

          通常讓這些控件加載圖片的代碼如下:
          JButton addTebBtn = new JButton(new ImageIcon(TabbedPanel.class
              .getResource("/addTab.gif")));

          如果要顯示動態Gif圖片這樣做法就不靈了.如果要顯示動態Gif圖片的話,我們需要從JLabel,JButton等控件繼承一個類,并重載其public void paint(Graphics g)方法,然后用一個線程不斷去刷新它(用Timer也可以,請參考文章" 封裝完畢,能顯示當前時間并改變風格的菜單類 ( http://www.aygfsteel.com/sitinspring/archive/2007/06/08/122753.html )"中Timer 的做法,它有少實現一個Runnable接口的優勢),這樣gif的動態效果就顯示出來了.

          標簽的完整代碼如下,其它控件大家可自行參照實現:

          package com.junglesong.common.component.label;

          import java.awt.Graphics;
          import java.awt.Graphics2D;
          import java.awt.Image;

          import javax.swing.JLabel;

          public class DynGifLabel extends JLabel implements Runnable {
              
          private static final long serialVersionUID = 45345345355L;

              
          // 用以存儲Gif動態圖片
              public Image image;

              
          // 用以刷新paint函數
              Thread refreshThread;

              
          /**
               * 
               * 
          @param image:
               *   Sample:new ImageIcon(DynGifLabel.class
               *            .getResource("/picture.gif")).getImage()
               
          */

              
          public DynGifLabel(Image image) {
                  
          this.image = image;
                  refreshThread 
          = new Thread(this);
                  refreshThread.start();
              }


              
          /**
               * 重載paint函數
               
          */

              
          public void paint(Graphics g) {
                  
          super.paint(g);
                  Graphics2D graph 
          = (Graphics2D) g;
                  
          if (image != null{
                      
          // 全屏描繪圖片
                      graph.drawImage(image, 00, getWidth(), getHeight(), 00, image
                              .getWidth(
          null), image.getHeight(null), null);
                  }

              }


              
          /**
               * 隔100毫秒刷新一次
               
          */

              
          public void run() {
                  
          while (true{
                      
          this.repaint();// 這里調用了Paint
                      try {
                          Thread.sleep(
          100);// 休眠100毫秒
                      }
           catch (Exception e) {
                          e.printStackTrace();
                      }

                  }

              }

          }


          使用過程如下:
          DynGifLabel stateLbl = new DynGifLabel(new ImageIcon(ThreadPanel.class
                          .getResource(
          "/startThread.gif")).getImage());

          以上.

          posted on 2007-09-08 09:42 sitinspring 閱讀(8627) 評論(4)  編輯  收藏 所屬分類: Swing

          評論

          # re: 如何讓Swing控件如JLabel,JButton等顯示動態Gif圖片 2007-09-08 12:22 someone

          用線程每隔一段時間刷新視圖來更新GIF的幀數會不出現與GIF自己的幀變化速度不一致的情況?
          http://www.kcmultimedia.com/gifcanvas/
          這個網站的代碼實現了你說的這個功能  回復  更多評論   

          # re: 如何讓Swing控件如JLabel,JButton等顯示動態Gif圖片 2007-09-08 18:23 sitinspring

          謝謝樓上指點.  回復  更多評論   

          # re: 如何讓Swing控件如JLabel,JButton等顯示動態Gif圖片 2007-09-09 00:48 ruislan

          不客氣,多多交流,我也在這里開張了,以后在下就不用someone這種藏頭露尾的方式來交流了。  回復  更多評論   

          # re: 如何讓Swing控件如JLabel,JButton等顯示動態Gif圖片[未登錄] 2009-05-19 11:13 xy

          你太有才了,謝謝了  回復  更多評論   

          sitinspring(http://www.aygfsteel.com)原創,轉載請注明出處.
          主站蜘蛛池模板: 临海市| 新宾| 蒙阴县| 双桥区| 当雄县| 马边| 扎鲁特旗| 莱州市| 兰州市| 东海县| 桃江县| 白城市| 广州市| 宕昌县| 靖边县| 金堂县| 威宁| 文化| 丁青县| 铜川市| 健康| 类乌齐县| 邯郸县| 黔东| 当阳市| 招远市| 平谷区| 民权县| 黄浦区| 永靖县| 二连浩特市| 营口市| 曲松县| 秦皇岛市| 绥宁县| 达日县| 山阳县| 广平县| 乌兰察布市| 邵武市| 合川市|