使用Swing创建幻灯片

时间:2014-07-09 07:16:09

标签: java image swing animation slideshow

无法以较小的时间间隔逐个绘制图像。 。

package SwingsTraining;


import java.io.*;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
class SlideShowDemo
{
        JFrame frame;

        JPanel panel1;
        JPanel panel2;
        JPanel panel3;

        JLabel label;

        JButton pick;
        JButton start;

        JFileChooser choose;

        String path="";
        String parent="";

        ImageIcon img;
        ImageIcon img1[]=new ImageIcon[100];
        static int i = 0;

        public void go()
        {
                frame= new JFrame();
                choose=new JFileChooser();

                panel1= new JPanel();
                panel2= new JPanel();
                panel3= new JPanel();

                pick=new JButton("choose file");
                panel1.add(pick);

                start=new JButton("start");
                panel3.add(start);

                label = new JLabel();
                panel2.add(label);

                frame.add(BorderLayout.NORTH,panel1);
                frame.add(BorderLayout.CENTER,panel2);
                frame.add(BorderLayout.SOUTH,panel3);

                pick.addActionListener(new ActionListener()
                        {
                                public void actionPerformed(ActionEvent ae)
                                {
                                        int ret=choose.showOpenDialog(null);
                                        if(ret==choose.APPROVE_OPTION)
                                        {
                                                path=choose.getSelectedFile().getAbsolutePath();
                                                parent=choose.getSelectedFile().getParent();
                                                System.out.println(path);
                                        }
                                }
                        });

                start.addActionListener(new ActionListener()
                        {
                                public void actionPerformed(ActionEvent ae)
                                {
                                        File f=new File(parent);
                                        File file[]=f.listFiles();  
                                        Thread t = Thread.currentThread();
                                        for(int j=0;j<file.length;j++)
                                        {
                                            String filename = file[i].getAbsolutePath();
                                            ImageIcon im = new ImageIcon(filename);
                                            Image img = im.getImage();
                                            img = img.getScaledInstance(400, 400, 0);
                                            im.setImage(img);
                                            label.setIcon(im);
                                           panel2.repaint();

                                         }  
                                  }
                        });

                frame.setSize(500,500);
                frame.setVisible(true);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        }
        public static void main(String[] args)
        {
                SlideShowDemo slide=new SlideShowDemo();
                slide.go();
        }
}

0 个答案:

没有答案