欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JFileChooser实现对选定文件夹内图片自动播放和暂停播放实例代码

程序员文章站 2024-02-25 16:21:33
本案例通过使用jfilechooser实现对选定文件夹内图片实现自动播放和暂停播放 代码如下,如有不合适的地方 还请指教 package com.xiaoqia...

本案例通过使用jfilechooser实现对选定文件夹内图片实现自动播放和暂停播放

代码如下,如有不合适的地方 还请指教

package com.xiaoqiang;
import java.awt.container;
import java.awt.flowlayout;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.io.file;
import javax.swing.imageicon;
import javax.swing.jbutton;
import javax.swing.jfilechooser;
import javax.swing.jframe;
import javax.swing.jlabel;
import java.util.timer;
import java.util.timertask;
/**
 * 本例通过jfilechooser选择文件夹 
 * 对文件夹内图片进行滚动播放 用到timertask以及actionlistener
 * 详细解释jfilechooser使用及图片滚动过程需要的思维
 * @author xiaoqiang
 * @timer 2017年4月27日  
 */
public class playpicture extends jframe implements actionlistener{
  private file filedirectory;
  private jfilechooser filechooser;
  private container con;
  private jbutton nextpic;
  private jbutton previouspic;
  private jbutton showpic;
  private jbutton beginplaypic;
  private jbutton stopplaypic;
  private jlabel picicon;
  private string[] filename;
  private string parentpath;
  private static boolean play;
  private static playpicture playpicture;
  private int i=-1;
  private playpicture(){
    super("图片自动播放器");
    this.draw();
  }
  /**
   * 获取单例类 
   * 用于timertask执行时调用同一对象
   * @return object
   */
  public static object getinstance(){
    if(playpicture==null)
      playpicture=new playpicture();
    return playpicture;
  }
  /**
   * 画图方法 将gui画出来
   * 因为练习图片滚动和jfilechooser
   * 所以gui比较难看
   */
  public void draw(){
    con=this.getcontentpane();
    con.setlayout(new flowlayout());
    showpic=new jbutton("请选择目录");
    con.add(showpic);
    showpic.addactionlistener(this);
    picicon=new jlabel("请选择目录展示图片");
    con.add(picicon);
    previouspic=new jbutton("上一张");
    con.add(previouspic);
    previouspic.addactionlistener(this);
    nextpic=new jbutton("下一张");
    con.add(nextpic);
    nextpic.addactionlistener(this);
    beginplaypic=new jbutton("开始自动播放");
    stopplaypic=new jbutton("停止自动播放");
    con.add(beginplaypic);
    con.add(stopplaypic);
    beginplaypic.addactionlistener(this);
    stopplaypic.addactionlistener(this);
    this.setlocation(550, 200);
    this.setsize(900,700);
    this.setvisible(true);
  }
  /**
   * 执行自动播放效果 
   * 注意使用的单例类
   * 暂停的话设置单例类静态变量
   * play为false
   */
  public void automatic(){
    timertask task = new timertask() { 
      @override 
      public void run() {
        ((playpicture) playpicture.getinstance()).nextpicture();
        if(!((playpicture)playpicture.getinstance()).play){
          ((playpicture) playpicture.getinstance()).previouspicture();
        }
      } 
    }; 
    timer timer = new timer(); 
    long delay = 0; 
    //intevalperiod为秒数
    long intevalperiod = 5 * 1000; 
    timer.scheduleatfixedrate(task, delay, intevalperiod);
  }
  @override
  public void actionperformed(actionevent e) {
    if(e.getsource().equals(showpic)){
      //设置g盘为默认打开路径
      filechooser=new jfilechooser(new file("g:"));
      /*
       * 设置可以选择文件夹,默认为只允许选择文件
       * 
       * directories_only 指示仅显示目录。
       * files_and_directories 指示显示文件和目录
       * files_only 指示仅显示文件。(默认)
       * 
       */
      filechooser.setfileselectionmode(jfilechooser.directories_only);
      //把jfilechooser释放
      filechooser.showopendialog(this);
      filedirectory=filechooser.getselectedfile();
      i=-1;//每次打开都将i重置 方便播放文件
      parentpath=filedirectory.getabsolutepath();
      filename=filedirectory.list();
      if(haspicture()){
        this.nextpicture();
        setbottonenabled(true);
      }else{
        picicon.settext("该目录没有图片哦");
        picicon.seticon(null);
        setbottonenabled(false);
      }
    }else if(e.getsource().equals(nextpic)){
      this.nextpicture();
    }else if(e.getsource().equals(previouspic)){
      this.previouspicture();
    }else if(e.getsource().equals(beginplaypic)){
      this.automatic();
      play=true;
    }else if(e.getsource().equals(stopplaypic)){
      play=false;
    }
  }
  //设置按钮不可用
  private void setbottonenabled(boolean available){
    nextpic.setenabled(available);
    previouspic.setenabled(available);
    beginplaypic.setenabled(available);
    stopplaypic.setenabled(available);
  }
  //判断所选路径是否有图片
  private boolean haspicture(){
    for(string s:filename){
      if(s.matches("(?i).*(.jpg|.png|.gif|.bpm|.jpeg)$"))
        return true;
    }
    return false;
  }
  private void previouspicture(){
    if(i==0){
      i=filename.length-1;
    }
    while(!filename[--i].matches("(?i).*(.jpg|.png|.gif|.bpm|.jpeg)$")){
      if(i==-1){
        i=filename.length;
      }
    }
    system.out.println(i);
    picicon.seticon(new imageicon(parentpath+"\\"+filename[i]));
    picicon.settext("");
  }
  private void nextpicture() {
    if(i==filename.length-1){
      i=0;
    }
    while(!filename[++i].matches("(?i).*(.jpg|.png|.gif|.bpm|.jpeg)$")){
      if(i==filename.length-1){
        i=-1;
      }
    }
    system.out.println(i);
    picicon.seticon(new imageicon(parentpath+"\\"+filename[i]));
    picicon.settext("");
  }
  public static void main(string[] args) {
    //获取实例调用构造方法
    playpicture.getinstance();
  }
}

以上所述是小编给大家介绍的jfilechooser实现对选定文件夹内图片自动播放和暂停播放实例代码,希望对大家有所帮助