循环显示照片

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");
 }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值