JFileChooser实现对选定文件夹内图片自动播放和暂停播放实例代码
作者:xiaoqiang1001 发布时间:2021-10-02 15:41:18
标签:java,jfilechooser,播放
本案例通过使用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实现对选定文件夹内图片自动播放和暂停播放实例代码网站的支持!
来源:http://blog.csdn.net/crpxnmmafq/article/details/70876399


猜你喜欢
- 前言我们知道移动应用页面跳转是非常重要的一部分,几乎我们的程序和用户打交道的就是页面,或者叫view,我们Android基本都是Activi
- 前言从来没接触过flutter,我将在这里记录下我的flutter学习踩坑历程。安装就从安装开始叭,官网链接在此。先遵照官网教程来叭。系统要
- 由于项目没有设计返回键,一旦进入别的应用,就无法回到桌面。只能通过串口输入input keyevent 4(返回键)来返回桌面,为了方便调试
- 本文实例为大家分享了C#实现会移动的文字效果的具体代码,供大家参考,具体内容如下1 题目描述(1)Form1窗体设计界面如下:(2)窗体左侧
- 用java swing写的一个简单的五子棋游戏。下面是Main.java。package com.crossing.main;import
- protobuf 是 google的一个开源项目,可用于以下两种用途:(1)数据的存储(序列化和反序列化),类似于xml、json等;(2)
- 网络中数据传输经常是xml或者json,现在做的一个项目之前调其他系统接口都是返回的xml格式,刚刚遇到一个返回json格式数据的接口,通过
- 使用异步包(推荐)async包由 Dart 编程语言的作者开发和发布。它提供了dart:async风格的实用程序来增强异步计算。可以帮助我们
- 方式一public class Test{ public static void main(String[] args) throws Ex
- 这个功能,大家也都可以去百度以下,千篇一律都自己写的(抄的)封装好的公共类,此处还是得膜拜下原创的大佬,可以花时间去搞这个,我看着都头皮发麻
- clone() 方法麻烦一些,需要将所有涉及到的类实现声明式接口 Cloneable,并覆盖Object类中的clone()方法,并设置作用
- 网上很多资料在描述Java内存模型的时候,都会介绍有一个主存,然后每个工作线程有自己的工作内存。数据在主存中会有一份,在工作内存中也有一份。
- 由于毕业后工作没有对接到专业问题,导致四五年没有碰过Winform程序了。突然由于工作问题,为了方便自己,所以想自己写写小winform小软
- 本文实例讲述了Android结束进程的方法。分享给大家供大家参考,具体如下:最近在做一个类似与任务管理器的东西,里面有个功能,可以通过这个管
- 实例如下所示:System.Net.WebClient myWebClient = new System.Net.WebClient();
- 概述从今天开始, 小白我将带大家开启 Java 数据结构 & 算法的新篇章.链表链表 (Linked List) 是一种递归的动态数
- 给对象按照字符串属性进行排序在java中对象进行排序,排序的属性是string,我们只需要实现Comparator接口,然后实现比较的方式。
- 这篇文章主要介绍了如何通过Java实现时间轴过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友
- 本文实例讲述了java生成xml格式文件的方法。分享给大家供大家参考,具体如下:这里演示利用Java生成xml格式文件Demo中所用到的ja
- 功能描述1、创建扑克牌。包括四种花色(黑桃,红心,梅花,方块),十三种点数(2-10,J,Q,K),不考虑大小王。2、创建两个玩家。包括玩家