图片浏览器Java课程设计Word格式文档下载.docx
《图片浏览器Java课程设计Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《图片浏览器Java课程设计Word格式文档下载.docx(32页珍藏版)》请在冰豆网上搜索。
![图片浏览器Java课程设计Word格式文档下载.docx](https://file1.bdocx.com/fileroot1/2023-1/22/4c5eab99-29a6-4a2a-bf8e-f35106e1a506/4c5eab99-29a6-4a2a-bf8e-f35106e1a5061.gif)
JButton[]jb;
按钮数组
privateJTreetree;
文件夹选择树
privateJScrollPanejsp1,jsp2,jsp3;
滚动面板
Timertimer;
计时器
publicPictureDir()类的构造函数
WindowAction窗体事件
TreeSelect文件夹选择树事件
FileDidianListener分类为地点事件
FileRenwuListener分类为人物事件
FileXiangceListener分类为相册事件
FileQitaListener分类为其它事件
FileZipListener备份事件
FileBackListener还原事件
MouseAction鼠标点击事件
FileStartListener幻灯片播放事件
JCAction显示分类图片事件
read(String)读文件函数
save(String,String)写文件函数
unzip(String,String)解压缩函数
resizeIcon(ImageIcon,JLabel)控制图片显示大小的函数
deleteFile(File)删除文件函数
main(String[])主函数
2主要代码
整个程序的界面如下图:
2.1选择文件夹功能
classTreeSelectimplementsTreeSelectionListener{
publicvoidvalueChanged(TreeSelectionEvente){
TreePathpath=e.getPath();
DefaultMutableTreeNodenode=(DefaultMutableTreeNode)path.getLastPathComponent();
ObjectuserObject=node.getUserObject();
if(!
(userObjectinstanceofFile)){
return;
}
Filefolder=(File)userObject;
folder.isDirectory())
File[]files1=initImageSets1(folder);
for(Filefile:
files1){
node.add(newDefaultMutableTreeNode(file));
files2=initImageSets2(folder);
for(inti=0;
i<
imageLabel.length;
i++){
imageLabel[i].setIcon(null);
imageLabel[i].setBorder(null);
files2.length;
imageLabel[i].setIcon(newImageIcon(files2[i].toString()));
imageLabel[i].addMouseListener(newMouseAction());
imageLabel[i].setName(String.valueOf(i));
resizeIcon(newImageIcon(files2[i].toString()),imageLabel[i]);
}
privateFile[]initImageSets1(FilepictureDir){
returnpictureDir.listFiles(newFilenameFilter(){
publicbooleanaccept(Filedir,Stringname){
if(!
newFile(dir,name).isFile())
returntrue;
else
returnfalse;
}
});
privateFile[]initImageSets2(FilepictureDir){
StringlowserName=name.toLowerCase();
if(lowserName.endsWith("
jpg"
)||lowserName.endsWith("
jpeg"
)
||lowserName.endsWith("
gif"
png"
GIF"
TIFF"
ICO"
BMP"
)){
}
returnfalse;
}
结果:
2.2图片分类功能
classFileDidianListenerimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
try{
save(pt,"
didian.txt"
);
JOptionPane.showMessageDialog(null,"
分类为“地点”完成!
"
}catch(Exceptione1){
e1.printStackTrace();
}
classFileRenwuListenerimplementsActionListener{
renwu.txt"
分类为“人物”完成!
privateDefaultMutableTreeNoderootNode;
classFileXiangceListenerimplementsActionListener{
xiangce.txt"
分类为“相册”完成!
classFileQitaListenerimplementsActionListener{
qita.txt"
分类为“其它”完成!
2.3图片备份和还原功能
classFileZipListenerimplementsActionListener{
inta=0;
Filefile1=newFile(pt);
Filefile2=newFile("
imagesdata.zip"
Filef=newFile("
D:
/1/3/5"
f.mkdirs();
if(!
file2.exists()){
file2.createNewFile();
a=1;
else
unzip("
"
FileOutputStreamfos=newFileOutputStream(file2);
ZipOutputStreamzos=newZipOutputStream(fos);
byte[]buffer=newbyte[1024];
if(a==0){
File[]files=f.listFiles();
File[]filess=newFile[files.length+1];
for(inti=0;
files.length;
filess[i]=files[i];
filess[filess.length-1]=file1;
for(inti=0;
i<
filess.length;
i++){
String[]st=filess[i].getName().split("
."
for(intj=0;
j<
st.length;
j++){
JOptionPane.showMessageDialog(null,j+"
="
+st[j]);
}
ZipEntryentry=newZipEntry(newDate().getTime()+filess[i].getName());
FileInputStreamin=newFileInputStream(filess[i]);
zos.putNextEntry(entry);
intlen;
while((len=in.read(buffer))>
0){
zos.write(buffer,0,len);
zos.closeEntry();
in.close();
if(a==1){
ZipEntryentry=newZipEntry(newDate().getTime()+file1.getName());
FileInputStreamfis=newFileInputStream(file1);
zos.putNextEntry(entry);
intread=0;
while((read=fis.read(buffer))!
=-1){
zos.write(buffer,0,read);
zos.closeEntry();
fis.close();
zos.close();
fos.close();
备份完成!
deleteFile(f);
备份失败!
/1/2/3"
classFileBackListenerimplementsActionListener{
JFileChooserchooser1;
chooser1=newJFileChooser();
chooser1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser1.setCurrentDirectory(newFile("
));
intresult=chooser1.showOpenDialog(newJFrame());
Filefile=chooser1.getSelectedFile();
if(result==JFileChooser.APPROVE_OPTION);
{
try{
unzip("
file.toString());
}catch(IOExceptione1){
}
2.4幻灯片播放功能
classFileStartListenerimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
JFramef;
JPanelj;
JButtonstartBtn;
JButtonstopBtn;
startBtn=newJButton("
继续"
startBtn.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventee){
if(timer!
=null)
timer.start();
stopBtn=newJButton("
暂停"
stopBtn.addActionListener(newActionListener(){
timer.stop();
f=newJFrame();
j=newJPanel();
f.setSize(800,700);
f.setLocation(300,50);
f.setVisible(true);
Containerc=f.getContentPane();
c.add(newJScrollPane(jimageLabel));
j.add(startBtn);
j.add(stopBtn);
c.add(j,"
South"
if(files2.length==0)
timer=null;
timer=newTimer(200,newActionListener(){
intindex=0;
publicvoidactionPerformed(ActionEventel){
jimageLabel.setIcon(newImageIcon(newImageIcon(files2[index].toString()).getImage().getScaledInstance(750,
600,Image.SCALE_SMOOTH)));
index++;
index%=files2.length;
}
timer.setCoalesce(true);
timer.setDelay(3*500);
timer.start();
五、调试与分析
1、首先遇到的问题是,如何在同一个面板上显示多张图片?
解决办法:
将多个用于显示图片的JLabel控件,添加到JScollPanel控件中。
2、如何对图片进行分类?
当鼠标点击某一图片时,记录该图片的物理地址,并保存到特定的文件中。
3、如何向压缩文件中添加新的压缩文件?
先将原有的压缩文件解压到临时文件夹,然后将新文件与解压活的文件一起压缩。
六、功能测试
1、分类:
其它分类功能类同,不再一一显示;
2、备份:
3、幻灯片播放:
七、结论与心得
写这个程序时,遇到了很多问题,但是通过查阅资料以及网络搜索,大多都解决了;
最后只剩一个问题没有解决,那就是如何想一个压缩文件中添加新的文件;
我的方法是先将原有的压缩文件解压到临时文件夹,然后将新文件与解压活的文件一起压缩;
但是这样程序会浪费很多系统资源,我感觉这种方法不是太好,但是又找不到更好的方法,希望老师帮我解答。
八、参考资料
【1】Java开发实战1200例(第一卷)李钟尉陈丹丹清华大学出版社2011年1月
【2】Java项目开发案例全程实录(第二版)李钟尉陈丹丹清华大学出版社2011年1月
【3】Java程序设计案例教程王成端清华大学出版社2011年1月
【4】[美]BruceEckelPrenticeHalPTR著、陈昊鹏等译ThinkingInJava(第3版)机械工业出版社2005
附录(代码):
importjavax.swing.*;
importjavax.swing.Timer;
importjavax.swing.border.*;
importjavax.swing.event.*;
importjavax.swing.tree.*;
importjava.awt.event.*;
importjava.awt.*;
importjava.io.*;
import.URI;
importjava.util.*;
importjava.util.zip.*;
publicclassPictureDirextendsJFrame{
Stringpt;
File[]files2;
JLabel[]imageLabel;
JLabeljlabel,jimageLabel;
JPaneljp1,jp2,jp3;
JButton[]jb;
privateJTreetree;
privateJScrollPanejsp1,jsp2,jsp3;
Timertimer;
publicPictureDir(){
JMenuBarmenuBar=newJMenuBar();
setJMenuBar(menuBar);
JMenumenu=newJMenu("
文件"
menuBar.add(menu);
JMenuItemexitItem=newJMenuItem("
退出"
menu.add(exitItem);
exitItem.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvent
event){
System.exit(0);
});
JMenumenu1=newJMenu("
分类"
menuBar.add(menu1);
JMenuItemc1=newJMenuItem("
地点"
menu1.add(c1);
c1.addActionListener(newFileDidianListener());
JMenuItemc2=newJMenuItem("
人物"
menu1.add(c2);
c2.addActionListener(newFileRenwuListener());
JMenuItemc3=newJMenuItem("
相册"
menu1.add(c3);
c3.addActionListener(newFileXiangceListener());
JMenuItemc4=newJMenuItem("
其它"
menu1.add(c4);
c4.addActionListener(newFileQitaListener());
JMenumenu2=newJMenu("
数据库"
menuBar.add(menu2);
JMenuItems1=newJMenuItem("
备份"
menu2.add(s1);
s1.addActionListener(newFileZipListener());
JMenuItems2=newJMenuItem("
还原"
menu2.add(s2);
s2.addActionListener(newFileBackList