请问怎么样用GUI实现图片切换的功能,简单的幻动片制作,每隔几秒钟,JLabel组件上就会出现不同的图片,
谢谢拉!

解决方案 »

  1.   

    使用线程应该可以吧,间隔时间就是你让thread.sleep()然后更换label的图片
      

  2.   

    可以用javax.swing.Timer,每隔一段时间出发一个事件,重新设置图片。int delay = 1000; //milliseconds
      ActionListener taskPerformer = new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
              //...Perform a task...
          }
      };
      new Timer(delay, taskPerformer).start();
      

  3.   

    package com.sysdynamic.slideshow;import java.util.List;
    import javax.swing.Icon;
    /**
     *
     * <p>Title:幻灯片的实现<接口></p>
     *
     * <p>Description: 相册中的相片以幻灯片的形式查看<接口></p>
     *
     * <p>Copyright: Copyright (c) 2007</p>
     *
     * <p>Company: 友升</p>
     *
     * @author Andy
     * @version 1.0
     */
    public interface ISlideShow {
        
        /**
         * 下一张
         * @return Icon
         */
        public Icon forward();
        
        /**
         * 上一张
         * @return Icon
         */
        public Icon backward();
        
        /**
         * 第一张
         * @return Icon
         */
        public Icon first();
        
        /**
         * 最后一张
         * @return Icon
         */
        public Icon last();
        
        public Icon getCurrentIcon();
        
        /**
         * 根据文件路径获得图片数组
         * @param imagePath String
         * @return String[]
         */
        
        public String[] imageArray(String imagePath);
        
        public String[] imageArray(List imageList);
        
        /**
         * 根据序号从图片数据中取了图片
         * @param j int
         * @return Icon
         */
        public Icon fileName(int j);
        
        
        /**
         * 是否是图片格式
         * @param str String
         */
        
        public boolean comperFile(String str);
        
        
        /**
         * 根据图片文件名读取图片
         * @param str String
         * @return Icon
         */
        public Icon showImage(String str) ;
        
    }
      

  4.   

    /*
     * IBasicSlideShow.java
     *
     * Created on 2007年11月8日, 下午9:39
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */package com.sysdynamic.slideshow;import java.util.List;/**
     *
     * @author Andy Yang
     */
    public interface IBasicSlideShow {
        int close();    /**
         * 放映幻灯片
         */
        void run();    /**
         *  开始幻灯片播放
         */
        void starts();    void starts(List imageList);    /**
         * 暂停幻灯播放
         */
        void stop();
        
    }
      

  5.   

    package com.sysdynamic.slideshow;import java.awt.Image;
    import java.io.IOException;
    import java.util.List;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.Icon;
    import java.awt.Toolkit;
    import java.io.File;
    import java.awt.Dimension;/**
     *
     * <p>Title:幻灯片的实现类 </p>
     *
     * <p>Description:相册中的相片以幻灯片的形式查看 </p>
     *
     * <p>Copyright: Copyright (c) 2007</p>
     *
     * <p>Company: 友升</p>
     *
     * @author Andy Yang
     * @version 1.0
     */public class SlideShow implements ISlideShow {
        
        Thread threads; //自动播放幻灯片线程
        
        private String imageFilePath; //文件的路径
        
        int currentIndex = 0; //当前图片数组中的索引
        
        Icon currentIcon; // 当前的图片
        
        int imageFileSize = 0; //图片数组的图片的才度
        
        String[] photoNames = null;
        
        public final String[] imageType = {"jpg", "jpeg", "gif", "bmp",
        "GIF", "JPG", "JPEG", "BMP", "png"}; //图片格式数组
        
        /**
         * 无参构造方法
         */
        public SlideShow() {
        }
        
        /**
         * 有参构造方法
         * @param filePath String 文件路径
         */
        public SlideShow(String filePath) {
            this.imageFilePath = filePath;
            this.setPhotoNames(imageArray(imageFilePath));
        }
        
        public SlideShow(List imageList) {
            
            this.setPhotoNames(imageArray(imageList));
        }
        
        /**
         * 下一张
         * @return Icon
         */
        public Icon forward() {
            if (currentIndex < this.imageFileSize - 1) { 
                currentIcon = fileName(++currentIndex);
            } else {
                this.first();
            }
            return currentIcon;
        }
        
        /**
         * 上一张
         * @return Icon
         */
        public Icon backward() {
            if (currentIndex > 0) {
                currentIcon = fileName(--currentIndex);
            } else {
                currentIcon = this.last();
                currentIndex=this.imageFileSize;
            }
            return currentIcon;
        }
        
        /**
         * 第一张
         * @return Icon
         */
        public Icon first() {
            currentIndex = 0;
            currentIcon = fileName(currentIndex);
            return currentIcon;
        }
        
        /**
         * 最后一张
         * @return Icon
         */
        public Icon last() {
            currentIndex = this.imageFileSize;
            currentIcon = fileName(currentIndex - 1);
            return currentIcon;
        }
        
        /**当前张
         *
         * @return Icon
         */
        public Icon getCurrentIcon() {
            return this.currentIcon;
        }
        
        /**
         * 根据文件路径获得图片数组
         * @param imagePath String
         * @return String[]
         */
        
        public String[] imageArray(String imagePath) {
            
            File file = new File(imagePath);
            
            String[] filename = file.list();
            
            String[] filenames = new String[filename.length];
            
            int j = 0;
            
            for (int i = 0; i < filename.length; i++) {
                if (comperFile(filename[i])) {
                    filenames[j] = filename[i];
                    j++;
                }
            }
            this.imageFileSize = j;
            return filenames;
        }
        
        public String[] imageArray(List imageList){
            String[] filenames = new String[imageList.size()];
            int j = 0;
            for (int i = 0; i < imageList.size(); i++) { 
                if (comperFile(imageList.get(i).toString())) {
                    filenames[j] = imageList.get(i).toString();
                    j++;
                }
            }
            this.imageFileSize = j;
            return filenames;
        }
        
        /**
         * 根据序号从图片数据中取了图片
         * @param j int
         * @return Icon
         */
        public Icon fileName(int j) {
            String name = null;
            name = photoNames[j];
            return showImage(name);
        }
        
        
        /**
         * 是否是图片格式
         * @param str String
         */
        
        public boolean comperFile(String str) {
            if (str.lastIndexOf(".") > 0) {
                String six = str.substring(
                        str.indexOf(".") + 1);
                for (String s : imageType) {
                    if (s.equalsIgnoreCase(six.trim())) {
                        return true;
                    }
                }
            }
            return false;
        }
        
        
        /**
         * 根据图片文件名读取图片
         * @param str String
         * @return Icon
         */
        public Icon showImage(String str) {
            
            //   if (comperFile(str)) {
            try {
                Image image = ImageIO.read(new File(str));
                Icon icon = new ImageIcon(image);
                return icon;
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            return null;
        }
        
        public void setPhotoNames(String[] photonames) {
            this.photoNames = photonames;
        }
        
        public String[] getPhotoNames() {
            return this.photoNames;
        }
    }
      

  6.   

    package com.sysdynamic.slideshow;import javax.swing.*;
    import java.awt.*;
    import java.util.List;
    import java.awt.BorderLayout;
    import java.awt.event.KeyListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseAdapter;/**
     * <p>Title: 幻灯片的实现类<GUI></p>
     *
     * <p>Description:相册中的相片以幻灯片的形式查看<GUI> </p>
     *
     * <p>Copyright: Copyright (c) 2007</p>
     *
     * <p>Company: </p>
     *
     * @author Andy Yang
     * @version 1.0
     */
    public class BasicSlideShow extends JFrame implements KeyListener, Runnable, IBasicSlideShow { private static final long serialVersionUID = 1L;
    BorderLayout borderLayout1;
        List albumObjects;
        JLabel imageLabel;
        Dimension screenSize;
        static ISlideShow slideShow;
        Thread threads;
        int falg = 0;
        BasicSlideShow bss;
        public BasicSlideShow() {
            borderLayout1 = new BorderLayout();
            imageLabel = new JLabel();
            screenSize = getGraphicsConfiguration().getBounds().getSize();
            this.albumObjects = albumObjects;
            try {
                jbInit();
            } catch (Exception ex) {
            }
        }    void showImage(Icon icons) {
            try {
                imageLabel.setIcon(icons);
            } catch (Exception ex) {
                ex.printStackTrace(System.err);
            }
        }    void jbInit() throws Exception {
            this.addKeyListener(this);
            setUndecorated(true);
            Container c = getContentPane();
            imageLabel.setHorizontalAlignment(0);
            c.setBackground(Color.black);
            c.addMouseListener(new BasicSlideShow_c_mouseAdapter(this));
            c.setLayout(borderLayout1);
            setSize(screenSize);
            c.add(imageLabel, java.awt.BorderLayout.CENTER);
        }    public static void main(String[] args) {
          //  slideShow = new SlideShow("F:\\新建文件夹"
          //          ); // "C:\\Users\\Public\\Pictures\\Sample Pictures"
          //  BasicSlideShow bss = new BasicSlideShow();
          //  bss.showImage(slideShow.first());
          //  bss.setVisible(true);
        }    public void starts(List imageList){
            slideShow = new SlideShow(imageList
                    );
            bss = new BasicSlideShow();
            bss.showImage(slideShow.first());
            bss.setVisible(true);
        }
        public int close() {
            int value=0;
          //  int value = JOptionPane.showOptionDialog(null, "From slides !",
            //        "Messages",
           //         0, 1, null, null, null);
            return value;
        }    public void keyPressed(KeyEvent e) {        switch (e.getKeyCode()) {
                case 27: // '\033'
                    int j = 0;
                    if (threads != null) {
                        this.stop();
                        j = 1;
                    }                if (close() == 0) {
                       this.setVisible(false);
                    } else {
                        if (j == 1) {
                            this.starts();
                        }
                    }
                    break;            case 10: // '\n'
                case 32: // ' '
                case 34: // '"'
                case 39: // '\''
                case 40: // '('
                    this.showImage(slideShow.forward()); //下一张
                    break;            case 8: // '\b'
                case 33: // '!'
                case 37: // '%'
                case 38: // '&'
                    this.showImage(slideShow.backward()); //上一张
                    break;            case 36: // '$'
                    this.showImage(slideShow.first()); //第一张
                    break;            case 35: // '#'
                    this.showImage(slideShow.last()); //最后一张
                    break;            case 122:
                    if (falg % 2 == 0) {
                        this.starts();
                        falg++;
                    } else {
                        this.stop();
                        falg--;
                    }                break;
            }
        }
        public void keyReleased(KeyEvent e) {
        }    public void keyTyped(KeyEvent e) {
        }    /**
         *  开始幻灯片播放
         */
        public void starts() {
            if (threads == null) {
                threads = new Thread(this);
                threads.start();
            }
        }    /**
         * 暂停幻灯播放
         */
        public void stop() {
            if (threads != null) {
                threads.stop();
            }
            threads = null;
        }    /**
         *放映幻灯片
         */
        public void run() {
            while (threads != null) {
                this.showImage(slideShow.forward());
                try {
                    threads.sleep(1000);
                } catch (InterruptedException ex) {
                    threads = null;
                }
            }
        }    /**
         * 单击事件上下放映
         * @param e MouseEvent
         */
        public void c_mouseClicked(MouseEvent e) {
            if (e.getButton() == 1) {
                this.showImage(slideShow.forward());
            } else if (e.getButton() == 3) {
                this.showImage(slideShow.backward());
            }
        }
    }
    class BasicSlideShow_c_mouseAdapter extends MouseAdapter {
        private BasicSlideShow adaptee;
        BasicSlideShow_c_mouseAdapter(BasicSlideShow adaptee) {
            this.adaptee = adaptee;
        }    public void mouseClicked(MouseEvent e) {
            adaptee.c_mouseClicked(e);
        }
    }
      

  7.   

     List list = this.getPhotosList();
                IBasicSlideShow ibss = new BasicSlideShow();
                if (list.size() >= 1) {
                    ibss.starts(list);
               }
    测试方法中加入以上代码:就ok了!!! 
     List list = this.getPhotosList();  
     一个图片集合自己改一下