import java.awt.FlowLayout;
import java.awt.Image;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ShowAlbum extends JFrame {
Image b;
public ShowAlbum(String s) {
boolean flagShow = true;
this.setLayout(new FlowLayout());
this.setBounds(300, 400, 258, 210);
this.setResizable(false);
this.setAlwaysOnTop(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//获取图片目录路径
File file = new File(s);
String[] picture = file.list();
int j, k;
int[] flag = new int[picture.length];
for (k = 0, j = 0; j < picture.length; j++) {
if (picture[j].toLowerCase().endsWith(".jpg")
|| picture[j].toLowerCase().endsWith(".jpeg")
|| picture[j].toLowerCase().endsWith(".gif")
|| picture[j].toLowerCase().endsWith(".png")) {
k++;
}
}
String[] pic = new String[k];
for (k = 0, j = 0; j < picture.length; j++) {
if (picture[j].toLowerCase().endsWith(".jpg")
|| picture[j].toLowerCase().endsWith(".jpeg")
|| picture[j].toLowerCase().endsWith(".gif")
|| picture[j].toLowerCase().endsWith(".png")) {
pic[k] = s + "//" + picture[j];
flag[k] = j;
k++;
}
}
//把图片循环加到JFrame
JLabel label = new JLabel();
while (true) {
for (int i = 0; i < pic.length; i++) {
ImageIcon a = new ImageIcon(pic[i]);
b = a.getImage();
/*循环时换JLabel,图片不会自动刷新
* 换JLabel里的图片,才能自动刷新
* JLabel label = new JLabel(new ImageIcon(b.getScaledInstance(
* 250, 170, Image.SCALE_DEFAULT)));
*/
label.removeAll();
label.setIcon(new ImageIcon(b.getScaledInstance(250, 170,
Image.SCALE_DEFAULT)));
label.update(getGraphics());
this.add(label);//在前面循环前加,图片也显示不出来
if (flagShow) {
this.setVisible(true);//必须放再label加进去后,才能显示图片
flagShow = !flagShow;
}
this.update(getGraphics());
try {
Thread.sleep(10000);
this.remove(label);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
new ShowAlbum("E://Pictures//photo");
}
}