界面如下:
程序如下:
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.border.*; import java.applet.*; public class mypicture extends JFrame implements ActionListener, MouseListener{ int b=0; int cn=0;//记录当前卡片布局中显示的是第几张图片 int listp=0;//记录图片列表中图片的点击事件(点击上一幅图的下标) int Factnum=0;//保存符合规定的实际图片数量 String Imagesum[];//用于保存当前正在展示的所有 图片 String ImageDisplayN=null;//保存当前卡片布局中正在展示的图片的名字 JPanel pn1;//用于存放图片 JPanel pnt;//用于存放导航图片 JPanel pnp[];//用于存每一个放小图片 JPanel psum;//存放所有小图片 JPanel pntile;//用于放置图标 boolean flag=false;//用于关闭幻灯片 boolean imagedrop=false;//记录是否点击过图片列表中的图片 //设置网格包布局 CardLayout cdly=new CardLayout(); GridBagLayout gridbag=new GridBagLayout(); GridBagConstraints c=new GridBagConstraints(); int MM=0;//用于Imagesum[]的下标 int NN=0;//pnp的下标 JButton addimagepoint; //以下是获得屏幕宽度和高度 Toolkit kit=Toolkit.getDefaultToolkit(); Dimension screenSize = kit.getScreenSize(); int width=screenSize.width; int height=screenSize.height; String firstimage=null ;//用来记录第一张被加入的图片 //构造函数 public mypicture(){ super("相册管理系统"); pn1=new JPanel(); pn1.setLayout(cdly); pnt=new JPanel(); } public void setLayout(){ //播放音乐 String s = "a.wav"; AudioClip clip =Applet.newAudioClip(getClass().getResource(s)); clip.loop(); Container ctn=getContentPane(); ctn.setLayout(new BorderLayout(10,5)); pn1.setBorder(new EtchedBorder()); pnt.setLayout(new BorderLayout()); pnt.setBorder(new EtchedBorder()); JLabel tt=new JLabel(" *******图片导航******* "); JPanel ti=new JPanel(); ti.add(tt); Color cor=new Color(255,208,232); ti.setBackground(cor); pnt.add(ti,BorderLayout.NORTH); File filel=new File("image"); //判段文件是否存在,不是就新建立一个 if(filel.exists()){ File[] fiseli=filel.listFiles(); JLabel [] labellist=new JLabel[fiseli.length]; JScrollPane[] jsp=new JScrollPane[fiseli.length]; ImageIcon [] imgiconlist=new ImageIcon[fiseli.length]; pnp =new JPanel[3000];//最多加3000张 psum=new JPanel(); psum.setLayout(gridbag); Imagesum =new String[3000];//最多加3000张 pnt.add(new JScrollPane(psum),BorderLayout.CENTER); for(int i=0;i<fiseli.length;i++){ String str=fiseli[i].getName(); int NODE=str.indexOf("."); String eq=str.substring(NODE+1); String iname=str.substring(0,NODE); //System.out.println(eq); if(eq.equals("jpg")){ if(NN==0){ firstimage=str; } Integer sss=new Integer(NN); String f=sss.toString(); Image imgp = Toolkit.getDefaultToolkit().getImage("image/"+str); Image imgn=imgp.getScaledInstance((width*7)/10,(height*77)/100,Image.SCALE_DEFAULT); //ImageIcon ii=new ImageIcon(imgn); imgiconlist[i]=new ImageIcon(imgn); JLabel jtu=new JLabel(imgiconlist[i]); jsp[i]=new JScrollPane(jtu); pn1.add(jsp[i],f); Image imgm=imgp.getScaledInstance(100,80,Image.SCALE_DEFAULT); imgiconlist[i]=new ImageIcon(imgm); JLabel j=new JLabel(imgiconlist[i]); JPanel jp=new JPanel(); jp.add(j); pnp[NN]=new JPanel(); pnp[NN].addMouseListener(this); pnp[NN].setLayout(new BorderLayout()); pnp[NN].add(jp,BorderLayout.CENTER); JLabel imagename=new JLabel(iname,JLabel.CENTER); pnp[NN].add(imagename,BorderLayout.SOUTH); c.insets=new Insets(4,2,2,2); c.ipadx=2; c.ipady=2; c.gridwidth=GridBagConstraints.REMAINDER; gridbag.setConstraints(pnp[NN],c); psum.add(pnp[NN]); Imagesum[MM]=str;//保存当前的图片名字 Factnum++; MM++; NN++; } else if(eq.equals("JPG")){ if(NN==0){ firstimage=str; } Integer sss=new Integer(NN); String f=sss.toString(); Image imgp = Toolkit.getDefaultToolkit().getImage("image/"+str); Image imgn=imgp.getScaledInstance((width*7)/10,(height*77)/100,Image.SCALE_DEFAULT); //ImageIcon ii=new ImageIcon(imgn); imgiconlist[i]=new ImageIcon(imgn); JLabel jtu=new JLabel(imgiconlist[i]); jsp[i]=new JScrollPane(jtu); pn1.add(jsp[i],f); Image imgm=imgp.getScaledInstance(100,80,Image.SCALE_DEFAULT); imgiconlist[i]=new ImageIcon(imgm); JLabel j=new JLabel(imgiconlist[i]); JPanel jp=new JPanel(); jp.add(j); pnp[NN]=new JPanel(); pnp[NN].addMouseListener(this); pnp[NN].setLayout(new BorderLayout()); pnp[NN].add(jp,BorderLayout.CENTER); JLabel imagename=new JLabel(iname,JLabel.CENTER); pnp[NN].add(imagename,BorderLayout.SOUTH); c.insets=new Insets(4,2,2,2); c.ipadx=2; c.ipady=2; c.gridwidth=GridBagConstraints.REMAINDER; gridbag.setConstraints(pnp[NN],c); psum.add(pnp[NN]); Imagesum[MM]=str;//保存当前的图片名字 Factnum++; MM++; NN++; } else if(eq.equals("GIF")){ if(NN==0){ firstimage=str; } Integer sss=new Integer(NN); String f=sss.toString(); Image imgp = Toolkit.getDefaultToolkit().getImage("image/"+str); Image imgn=imgp.getScaledInstance((width*7)/10,(height*77)/100,Image.SCALE_DEFAULT); //ImageIcon ii=new ImageIcon(imgn); imgiconlist[i]=new ImageIcon(imgn); JLabel jtu=new JLabel(imgiconlist[i]); jsp[i]=new JScrollPane(jtu); pn1.add(jsp[i],f); Image imgm=imgp.getScaledInstance(100,80,Image.SCALE_DEFAULT); imgiconlist[i]=new ImageIcon(imgm); JLabel j=new JLabel(imgiconlist[i]); JPanel jp=new JPanel(); jp.add(j); pnp[NN]=new JPanel(); pnp[NN].addMouseListener(this); pnp[NN].setLayout(new BorderLayout()); pnp[NN].add(jp,BorderLayout.CENTER); JLabel imagename=new JLabel(iname,JLabel.CENTER); pnp[NN].add(imagename,BorderLayout.SOUTH); c.insets=new Insets(4,2,2,2); c.ipadx=2; c.ipady=2; c.gridwidth=GridBagConstraints.REMAINDER; gridbag.setConstraints(pnp[NN],c); psum.add(pnp[NN]); Imagesum[MM]=str;//保存当前的图片名字 Factnum++; MM++; NN++; } else if(eq.equals("gif")){ if(NN==0){ firstimage=str; } Integer sss=new Integer(NN); String f=sss.toString(); Image imgp = Toolkit.getDefaultToolkit().getImage("image/"+str); Image imgn=imgp.getScaledInstance((width*7)/10,(height*77)/100,Image.SCALE_DEFAULT); //ImageIcon ii=new ImageIcon(imgn); imgiconlist[i]=new ImageIcon(imgn); JLabel jtu=new JLabel(imgiconlist[i]); jsp[i]=new JScrollPane(jtu); pn1.add(jsp[i],f); Image imgm=imgp.getScaledInstance(100,80,Image.SCALE_DEFAULT); imgiconlist[i]=new ImageIcon(imgm); JLabel j=new JLabel(imgiconlist[i]); JPanel jp=new JPanel(); jp.add(j); pnp[NN]=new JPanel(); pnp[NN].addMouseListener(this); pnp[NN].setLayout(new BorderLayout()); pnp[NN].add(jp,BorderLayout.CENTER); JLabel imagename=new JLabel(iname,JLabel.CENTER); pnp[NN].add(imagename,BorderLayout.SOUTH); c.insets=new Insets(4,2,2,2); c.ipadx=2; c.ipady=2; c.gridwidth=GridBagConstraints.REMAINDER; gridbag.setConstraints(pnp[NN],c); psum.add(pnp[NN]); Imagesum[MM]=str;//保存当前的图片名字 Factnum++; MM++; NN++; } else if(eq.equals("png")){ if(NN==0){ firstimage=str; } Integer sss=new Integer(NN); String f=sss.toString(); Image imgp = Toolkit.getDefaultToolkit().getImage("image/"+str); Image imgn=imgp.getScaledInstance((width*7)/10,(height*77)/100,Image.SCALE_DEFAULT); //ImageIcon ii=new ImageIcon(imgn); imgiconlist[i]=new ImageIcon(imgn); JLabel jtu=new JLabel(imgiconlist[i]); jsp[i]=new JScrollPane(jtu); pn1.add(jsp[i],f); Image imgm=imgp.getScaledInstance(100,80,Image.SCALE_DEFAULT); imgiconlist[i]=new ImageIcon(imgm); JLabel j=new JLabel(imgiconlist[i]); JPanel jp=new JPanel(); jp.add(j); pnp[NN]=new JPanel(); pnp[NN].addMouseListener(this); pnp[NN].setLayout(new BorderLayout()); pnp[NN].add(jp,BorderLayout.CENTER); JLabel imagename=new JLabel(iname,JLabel.CENTER); pnp[NN].add(imagename,BorderLayout.SOUTH); c.insets=new Insets(4,2,2,2); c.ipadx=2; c.ipady=2; c.gridwidth=GridBagConstraints.REMAINDER; gridbag.setConstraints(pnp[NN],c); psum.add(pnp[NN]); Imagesum[MM]=str;//保存当前的图片名字 Factnum++; MM++; NN++; } else if(eq.equals("PNG")){ if(NN==0){ firstimage=str; } Integer sss=new Integer(NN); String f=sss.toString(); Image imgp = Toolkit.getDefaultToolkit().getImage("image/"+str); Image imgn=imgp.getScaledInstance((width*7)/10,(height*77)/100,Image.SCALE_DEFAULT); imgiconlist[i]=new ImageIcon(imgn); JLabel jtu=new JLabel(imgiconlist[i]); jsp[i]=new JScrollPane(jtu); pn1.add(jsp[i],f); Image imgm=imgp.getScaledInstance(100,80,Image.SCALE_DEFAULT); imgiconlist[i]=new ImageIcon(imgm); JLabel j=new JLabel(imgiconlist[i]); JPanel jp=new JPanel(); jp.add(j); pnp[NN]=new JPanel(); pnp[NN].addMouseListener(this); pnp[NN].setLayout(new BorderLayout()); pnp[NN].add(jp,BorderLayout.CENTER); JLabel imagename=new JLabel(iname,JLabel.CENTER); pnp[NN].add(imagename,BorderLayout.SOUTH); c.insets=new Insets(4,2,2,2); c.ipadx=2; c.ipady=2; c.gridwidth=GridBagConstraints.REMAINDER; gridbag.setConstraints(pnp[NN],c); psum.add(pnp[NN]); Imagesum[MM]=str;//保存当前的图片名字 Factnum++; MM++; NN++; } } } else{ filel.mkdir();/// 新建一个image文件夹 } JPanel pn2=new JPanel(); pn2.add(new JLabel(" author: 计科系06级2班 曾辉(061102030)")); pn2.setBackground(Color.gray); pn2.setBorder(new EtchedBorder()); pntile=new JPanel(); pntile.setLayout(new GridLayout(0,1,3,3)); //pntile.setBackground(new Color(255,185,255)); Icon imageopen=new ImageIcon("icon/open.jpg"); Icon imageprevious=new ImageIcon("icon/previous.jpg"); Icon imagenext=new ImageIcon("icon/next.jpg"); Icon imagepoint=new ImageIcon("icon/point.jpg"); Icon imagedelect=new ImageIcon("icon/delect.jpg"); Icon imagehelp=new ImageIcon("icon/help.jpg"); JButton addimageopen=new JButton("添加",imageopen); JButton addimageprevious=new JButton("向前",imageprevious); JButton addimagenext=new JButton("向后",imagenext); addimagepoint=new JButton("幻灯片",imagepoint); JButton addimagedelect=new JButton("删除",imagedelect); JButton addimagehelp=new JButton("帮助",imagehelp); addimageopen.addActionListener(this); addimageprevious.addActionListener(this); addimagenext.addActionListener(this); addimagepoint.addActionListener(this); addimagedelect.addActionListener(this); addimagehelp.addActionListener(this); //添加空白 JLabel empty1=new JLabel(" ◆********◆ "); JLabel empty2=new JLabel(" ◆********◆ "); JLabel empty3=new JLabel(" ◆********◆ "); JLabel empty4=new JLabel(" ◆********◆ "); JLabel empty5=new JLabel(" ◆********◆ "); JLabel empty6=new JLabel(" ◆********◆ "); JLabel empty7=new JLabel(" ◆********◆ "); JLabel empty8=new JLabel(" ◆********◆ "); JLabel empty9=new JLabel(" ◆********◆ "); JLabel empty10=new JLabel(" ◆********◆ "); pntile.add(empty5); pntile.add(empty6); pntile.add(empty7); pntile.add(empty8); pntile.add(empty9); pntile.add(empty10); pntile.add(addimageopen); pntile.add(addimageprevious); pntile.add(addimagenext); pntile.add(addimagepoint); pntile.add(addimagedelect); pntile.add(addimagehelp); pntile.add(empty1); pntile.add(empty2); pntile.add(empty3); pntile.add(empty4); ctn.add(pntile,BorderLayout.EAST); ctn.add(pn1,BorderLayout.CENTER); ctn.add(pn2,BorderLayout.SOUTH); ctn.add(pnt,BorderLayout.WEST); } public static void main(String [] args) throws IOException{ mypicture pct=new mypicture(); Image img = new ImageIcon("a.gif").getImage(); //一个图片 pct.setIconImage(img); JMenuBar jmb=new JMenuBar(); pct.setJMenuBar(jmb); JMenu fileMenu=new JMenu("文件"); JMenu editMenu=new JMenu("编辑"); JMenu helpMenu=new JMenu("帮助"); JMenuItem a=new JMenuItem("添加"); a.addActionListener(pct); JMenuItem d=new JMenuItem("删除"); d.addActionListener(pct); JMenuItem e=new JMenuItem("退出"); e.addActionListener(pct); fileMenu.add(a); fileMenu.add(d); fileMenu.add(e); JMenuItem h=new JMenuItem("幻灯片"); h.addActionListener(pct); editMenu.add(h); JMenuItem s=new JMenuItem("帮助"); s.addActionListener(pct); helpMenu.add(s); jmb.add(fileMenu); jmb.add(editMenu); jmb.add(helpMenu); pct.setLayout();//布局 pct.setExtendedState(pct.MAXIMIZED_BOTH);// 窗口最大化 pct.setLocation(0,0); pct.show(); pct.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e){ String str; str=e.getActionCommand(); if(str.equals("向前")){ previous(); } else if(str.equals("向后")){ next(); } else if(str.equals("添加")){ add(); } else if(str.equals("删除")){ delect(); } else if(str.equals("幻灯片")||str.equals("关闭它")){ if(flag==false){ slide(); addimagepoint.setLabel("关闭它"); } else { addimagepoint.setLabel("幻灯片"); slide(); } } else if(str.equals("退出")){ System.exit(1); } else if(str.equals("帮助")){ help(); } } //以下是捕捉鼠标单击事件,其中只要的mouseClicked另外的方法不实现 public void mouseClicked(MouseEvent e){ //以下代码实现当单击左边小图片是在主窗口中显示相应的大图片 //同时将被单击的图片背景改变颜色,前一张被点击了的图片还原 //以前的颜色 if(imagedrop==true) pnp[listp].setBackground(new Color(240,240,240)); for(int z=0;z<Factnum+2;z++){ if(e.getSource()==pnp[z]){ Integer pps=new Integer(z); String oos=pps.toString(); pnp[z].setBackground(new Color(255,184,149)); cdly.show(pn1,oos); listp=z; ImageDisplayN=Imagesum[z]; cn=z; System.out.println(ImageDisplayN); break; } } imagedrop=true; } public void mouseEntered(MouseEvent e){ } public void mouseExited(MouseEvent e){ } public void mousePressed(MouseEvent e){ } public void mouseReleased(MouseEvent e){ } public void previous(){ cdly.previous(pn1); if(imagedrop==true) pnp[listp].setBackground(new Color(240,240,240)); imagedrop=false; if(cn==0){ cn=Factnum-1; ImageDisplayN=Imagesum[cn]; } else { cn=cn-1; ImageDisplayN=Imagesum[cn]; } System.out.println(ImageDisplayN); } public void next(){ cdly.next(pn1); if(imagedrop==true) pnp[listp].setBackground(new Color(240,240,240)); imagedrop=false; if(cn==Factnum-1){ cn=0; ImageDisplayN=Imagesum[cn]; } else { cn=cn+1; ImageDisplayN=Imagesum[cn]; } System.out.println(ImageDisplayN); } public void add(){ JFileChooser jfc=new JFileChooser(new File("image/")); jfc.showOpenDialog(this); //jfc.setMultiSelectionEnabled(true); { File qq=jfc.getSelectedFile(); File pp=new File("image/");//获得路径 String argss=qq.getName();//取得文件名 //String kk=qq.getPath(); // System.out.println(kk); //System.out.println(argss); try{ if(qq!=null){ boolean flag=false; for(int ee=0;ee<Factnum;ee++){ if(argss.equals(Imagesum[ee])) flag=true; } if(flag==false){ FileInputStream bin=new FileInputStream(qq); System.out.println(bin); FileOutputStream bout=new FileOutputStream("Image/"+argss); byte [] buf=new byte[bin.available()]; bin.read(buf); bout.write(buf); bin.close(); bout.close(); int node=argss.indexOf("."); String eq=argss.substring(node+1); String iname=argss.substring(0,node); if(eq.equals("JPG")||eq.equals("jpg")||eq.equals("gif")||eq.equals("GIF")||eq.equals("png")||eq.equals("PNG")) { MM=MM+1; NN=NN+1; cn=cn+1; Imagesum[MM]=argss;//保存当前的图片名字 Factnum++; Integer sss=new Integer(NN); String f=sss.toString(); Image imgp = Toolkit.getDefaultToolkit().getImage("image/"+argss); Image imgn=imgp.getScaledInstance((width*7)/10,(height*77)/100,Image.SCALE_DEFAULT); ImageIcon imgiconlist=new ImageIcon(imgn); JLabel jtu=new JLabel(imgiconlist); JScrollPane jsp=new JScrollPane(jtu); pn1.add(jsp,f); Image imgm=imgp.getScaledInstance(100,80,Image.SCALE_DEFAULT); ImageIcon UUS=new ImageIcon(imgm); JLabel j=new JLabel(UUS); JPanel jp=new JPanel(); jp.add(j); pnp[NN]=new JPanel(); pnp[NN].addMouseListener(this); pnp[NN].setLayout(new BorderLayout()); pnp[NN].add(jp,BorderLayout.CENTER); JLabel imagename=new JLabel(iname,JLabel.CENTER); pnp[NN].add(imagename,BorderLayout.SOUTH); c.insets=new Insets(4,2,2,2); c.ipadx=2; c.ipady=2; c.gridwidth=GridBagConstraints.REMAINDER; gridbag.setConstraints(pnp[NN],c); psum.add(pnp[NN]); cdly.show(pn1,f); pn1.validate();//调用此方法重新显示 pnt.validate();//调用此方法重新显示 } } else{ //System.out.println("哎呀 烦啊"); JDialog dlg=new JDialog(this,"提示!!",true); JPanel b=new JPanel(new GridLayout(1,3)); dlg.setLayout(new BorderLayout()); JLabel j1=new JLabel(" 此相片已经存在!!"); JLabel j2=new JLabel(""); JLabel j3=new JLabel(""); dlg.add(j1,BorderLayout.CENTER); dlg.add(b,BorderLayout.SOUTH); b.add(j2); b.add(j3); Toolkit kit=Toolkit.getDefaultToolkit(); Dimension screenSize = kit.getScreenSize(); int width=screenSize.width; int height=screenSize.height; dlg.setBounds(width/2,height/2,200,100); dlg.setVisible(true); } } } catch(IOException w){ System.out.println(w); } } //播放音乐 // String s = "a.wav"; // AudioClip clip =Applet.newAudioClip(getClass().getResource(s)); //clip.loop(); } public void delect(){ //一下三句是删除imag文件夹中的图片 if (ImageDisplayN==null) ImageDisplayN=firstimage; File qqr=new File("image/"+ImageDisplayN); System.out.println("当前显示图片是:"+ImageDisplayN); qqr.delete(); //以下是将所有的开始的变量初始化 以便重新布局pn1和pnt中的内容 firstimage=null; MM=0; NN=0; cn=0;//记录当前卡片布局中显示的是第几张图片 listp=0;//记录图片列表中图片的点击事件(点击上一幅图的下标) Factnum=0; ImageDisplayN=null; imagedrop=false; pn1.removeAll();//移除pn1中所有组件 pnt.removeAll();//移除pnt中所有组件 //pn1.repaint();//重绘 //pnt.repaint();//重绘 setLayout();//重新布局 pn1.validate();//调用此方法重新显示 pnt.validate();//调用此方法重新显示 } public void help(){ JDialog dg=new JDialog(this,"帮助",true); JPanel b=new JPanel(new GridLayout(6,1)); dg.setLayout(new BorderLayout()); JLabel j1=new JLabel(" 相册管理系统"); j1.setForeground(Color.blue); j1.setFont(new Font("宋体",Font.BOLD,15)); JLabel j2=new JLabel(""); JLabel j3=new JLabel(" 作者:曾辉"); JLabel j4=new JLabel(" 班级:06级2班"); JLabel j5=new JLabel(" 说明:没有什么好说的,做得太简单了"); JLabel j6=new JLabel(""); dg.add(b,BorderLayout.CENTER); //dlg.add(b,BorderLayout.SOUTH); b.add(j1); b.add(j2); b.add(j3); b.add(j4); b.add(j5); b.add(j6); //Toolkit kit=Toolkit.getDefaultToolkit(); // Dimension screenSize = kit.getScreenSize(); //int width=screenSize.width; //int height=screenSize.height; dg.setBounds(width/2,height/2,300,180); dg.setVisible(true); } //建造一个内部类(计数器要实现的代码)幻灯片的核心 ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent e) { next(); } }; Timer timer = new Timer(3000, listener);//创建一个计时器 public void slide(){ if(flag==false){ timer.start(); //计时器开始执行 flag=true; } else{ timer.stop();//关闭计时器 flag=false; } } }