如何在jlabel上进行图像随机播放?

时间:2014-03-24 17:21:41

标签: java swing timer imageicon

我正在创建一个Java桌面应用程序,我希望每3秒对图像进行一次洗牌。我能够做到这一点,但问题是我只想使用单个JLabel,其中所有图像每隔3秒进行一次洗牌,并且我有多个JLabel的代码

以下是我找到的代码here。我只想使用单JLabel。我怎样才能做到这一点?

/**
 * @see https://stackoverflow.com/a/22423511/230513
 * @see https://stackoverflow.com/a/12228640/230513
 */
public class ImageShuffle extends JPanel {

    private List<Icon> list = new ArrayList<Icon>();
    private List<JLabel> labels = new ArrayList<JLabel>();
    private Timer timer = new Timer(1000, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            update();
        }
    });

    public ImageShuffle() {
        this.setLayout(new GridLayout(1, 0));
        list.add(UIManager.getIcon("OptionPane.errorIcon"));
        list.add(UIManager.getIcon("OptionPane.informationIcon"));
        list.add(UIManager.getIcon("OptionPane.warningIcon"));
        list.add(UIManager.getIcon("OptionPane.questionIcon"));
        for (Icon icon : list) {
            JLabel label = new JLabel(icon);
            labels.add(label);
            this.add(label);
        }
        timer.start();
    }

    private void update() {
        Collections.shuffle(list);
        int index = 0;
        for (JLabel label : labels) {
            label.setIcon(list.get(index++));
        }
    }

    private void display() {
        JFrame f = new JFrame("ImageShuffle");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ImageShuffle().display();
            }
        });
    }
}

2 个答案:

答案 0 :(得分:3)

variationoriginal example有一些不幸(但也许是有教育意义的)问题:

  • 每次迭代都会创建一个Random的新实例;只需要一个。

  • 表达式r.nextInt(3) + 1从不选择list的第一个或最后一个元素。

  • 如果list的大小发生变化,使用数字文字可能会导致程序失败。

相反,shuffle() list并选择第一个元素。

private void update() {
    Collections.shuffle(list);
    label.setIcon(list.get(0));
}

经测试:

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;

/**
 * @see https://stackoverflow.com/a/22631012/230513
 * @see https://stackoverflow.com/a/22423511/230513
 * @see https://stackoverflow.com/a/12228640/230513
 */
public class ImageShuffle extends JPanel {

    private List<Icon> list = new ArrayList<Icon>();
    private JLabel label = new JLabel();
    private Timer timer = new Timer(1000, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            update();
        }
    });

    public ImageShuffle() {
        this.setLayout(new GridLayout(1, 0));
        list.add(UIManager.getIcon("OptionPane.errorIcon"));
        list.add(UIManager.getIcon("OptionPane.informationIcon"));
        list.add(UIManager.getIcon("OptionPane.warningIcon"));
        list.add(UIManager.getIcon("OptionPane.questionIcon"));
        label.setIcon(UIManager.getIcon("OptionPane.informationIcon"));

        timer.start();
    }

    private void update() {
        Collections.shuffle(list);
        label.setIcon(list.get(0));
    }

    private void display() {
        JFrame f = new JFrame("ImageShuffle");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.add(label);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ImageShuffle().display();
            }
        });
    }
}

答案 1 :(得分:1)

/**
 * @see http://stackoverflow.com/a/22616636/318599
 * @see http://stackoverflow.com/a/22423511/230513
 * @see http://stackoverflow.com/a/12228640/230513
 */
public class ImageShuffle extends JPanel {

   private List<Icon> list = new ArrayList<Icon>();
   private List<JLabel> labels = new ArrayList<JLabel>();
   private JLabel label = new JLabel();
   private Timer timer = new Timer(1000, new ActionListener() {

      @Override
       public void actionPerformed(ActionEvent e) {
        update();
       }
   });

   public ImageShuffle() {
       this.setLayout(new GridLayout(1, 0));
       list.add(UIManager.getIcon("OptionPane.errorIcon"));
       list.add(UIManager.getIcon("OptionPane.informationIcon"));
       list.add(UIManager.getIcon("OptionPane.warningIcon"));
       list.add(UIManager.getIcon("OptionPane.questionIcon"));
       label.setIcon(UIManager.getIcon("OptionPane.informationIcon"));

       timer.start();
   }

   private void update() {

    Random r=new Random();
    int i1=(r.nextInt(3) +1);

    label.setIcon(list.get(i1));
}

private void display() {
    JFrame f = new JFrame("ImageShuffle");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(this);
    f.add(label);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

           @Override
          public void run() {
              new ImageShuffle().display();
           }
       });
   }
 }