[英]How to image shuffle on jlabel?
I am creating a Java desktop application where I want to shuffle image in every 3 sec. 我正在创建一个Java桌面应用程序,我想每3秒对图像进行一次随机播放。 I am able to do this, but problem is that I want to use only single
JLabel
where all image shuffle in every 3 sec and I have code for multiple JLabel
我能够做到这一点,但是问题是我只想使用单个
JLabel
,每3秒就会出现一次所有图像混洗,并且我有多个JLabel
代码
Here is the code I found here . 这是我在这里找到的代码。 I want to use only single
JLabel
. 我只想使用单个
JLabel
。 How can I achieve this? 我该如何实现?
/**
* @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();
}
});
}
}
This variation of the original example has some unfortunate (but perhaps instructive) problems: 原始示例的这种变体存在一些不幸的(但可能是有益的)问题:
A new instance of Random
is created at each iteration; 每次迭代都会创建一个
Random
实例。 only one is required. 只需要一个。
The expression r.nextInt(3) + 1
never selects the first or last element of the list
. 表达式
r.nextInt(3) + 1
从不选择list
的第一个或最后一个元素。
The use of numeric literals may cause the program to fail if the size of the list
changes. 如果
list
大小更改,则使用数字文字可能会导致程序失败。
Instead, shuffle()
the list
and choose the first element. 而是
shuffle()
list
并选择第一个元素。
private void update() {
Collections.shuffle(list);
label.setIcon(list.get(0));
}
As tested: 经测试:
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();
}
});
}
}
/**
* @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();
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.