java-凯发k8官方网
                                                            凯发k8官方网
收集整理的这篇文章主要介绍了
                                java--图片浏览器
小编觉得挺不错的,现在分享给大家,帮大家做个参考.                        
                                功能:启动后选择打开文件,可以打开图片进行浏览。
v 1.0 :支持上一张 下一张功能。(欠缺,窗口大小未随着图片大小而改变)
1 import java.awt.borderlayout; 2 import java.awt.eventqueue; 3 import java.awt.event.actionevent; 4 import java.awt.event.actionlistener; 5 import java.io.file; 6 import java.io.ioexception; 7 8 import javax.imageio.imageio; 9 import javax.swing.imageicon; 10 import javax.swing.jbutton; 11 import javax.swing.jfilechooser; 12 import javax.swing.jframe; 13 import javax.swing.jlabel; 14 import javax.swing.jmenu; 15 import javax.swing.jmenubar; 16 import javax.swing.jmenuitem; 17 import javax.swing.joptionpane; 18 import javax.swing.jtoolbar; 19 20 public class testmenu extends jframe { 21 private jtoolbar toolbar = new jtoolbar(); 22 private jbutton up = new jbutton("up"); 23 private jbutton down = new jbutton("down"); 24 private jlabel label; 25 private jfilechooser chooser; 26 private static string[] currentpath = new string[10000];// 最多一万张图片 27 private static int dirphotocount = 0; 28 private static boolean flag = false; //user yes or not open dir 29 30 public static void getfilenametofullphotoarray(string dirpath) { 31 string path = dirpath; 32 file f = new file(path); 33 if (!f.exists()) { 34 system.out.println(path " not exists"); 35 return; 36 } 37 int cut = 0; 38 file fa[] = f.listfiles(); 39 for (int i = 0; i < fa.length; i ) { 40 file fs = fa[i]; 41 try { 42 if (imageio.read(fs) != null) { // yes or not photo style file. 43 currentpath[cut ] = dirpath "\\" fs.getname(); 44 } 45 } catch (ioexception ex) { 46 47 } 48 } 49 dirphotocount = cut; 50 } 51 52 public testmenu() { 53 super(); 54 settitle("photo viewer"); 55 getcontentpane().add(toolbar, borderlayout.north); 56 label = new jlabel(); 57 this.chooser = new jfilechooser();// java provide file selected 58 chooser.setcurrentdirectory(new file("."));// view path 59 60 jmenubar jmb = new jmenubar(); 61 setjmenubar(jmb); 62 jmenu filemenu = new jmenu("file"); 63 jmenuitem jmiexit = new jmenuitem("exit"); 64 jmenuitem jmiopen = new jmenuitem("open"); 65 jmb.add(filemenu); 66 toolbar.add(up); 67 toolbar.add(down); 68 getcontentpane().add(toolbar, borderlayout.north); 69 add(label); 70 jmb.add(filemenu); 71 filemenu.add(jmiopen); 72 filemenu.add(jmiexit); 73 74 up.addactionlistener(new actionlistener() { 75 public void actionperformed(actionevent e) { 76 77 if (flag) { 78 string dir = chooser.getselectedfile().getparent(); 79 getfilenametofullphotoarray(dir);// current dir build photo array 80 string currentimage = label.geticon().tostring(); 81 for (int i = 0; i < dirphotocount; i ) { 82 if (currentpath[i].equals(currentimage)) { 83 label.seticon(new imageicon(currentpath[i == 0 ? dirphotocount - 1 : i - 1])); 84 break; 85 } 86 } 87 } else { 88 joptionpane.showmessagedialog(null, "当前无图片,请选择正确文件", "打开失败", joptionpane.error_message); 89 90 } 91 } 92 93 }); 94 down.addactionlistener(new actionlistener() { 95 public void actionperformed(actionevent e) { 96 97 if (flag) { 98 string dir = chooser.getselectedfile().getparent(); 99 getfilenametofullphotoarray(dir);// 对当前目录建立图片数组 100 string currentimage = label.geticon().tostring(); 101 for (int i = 0; i < dirphotocount; i ) { 102 if (currentpath[i].equals(currentimage)) { 103 label.seticon(new imageicon(currentpath[i == dirphotocount - 1 ? 0 : i 1])); 104 break; 105 } 106 } 107 } else { 108 109 joptionpane.showmessagedialog(null, "当前无图片,请选择正确文件", "打开失败", joptionpane.error_message); 110 111 } 112 } 113 114 }); 115 116 jmiopen.addactionlistener(new actionlistener() { 117 public void actionperformed(actionevent e) { 118 int result = chooser.showopendialog(null); 119 if (result == jfilechooser.approve_option) { 120 try { 121 if (imageio.read(chooser.getselectedfile()) != null) { // 判断是否是图片文件 122 flag = true; 123 string name = chooser.getselectedfile().getpath(); 124 label.seticon(new imageicon(name)); 125 } else { 126 joptionpane.showmessagedialog(null, "请选择正确文件", "打开失败", joptionpane.error_message); 127 } 128 } catch (ioexception ex) { 129 130 } 131 132 } 133 } 134 }); 135 136 jmiexit.addactionlistener(new actionlistener() { 137 public void actionperformed(actionevent e) { 138 system.exit(0); 139 } 140 }); 141 } 142 143 public static void main(string args[]) { 144 eventqueue.invokelater(new runnable() {// using runnable anonymous 145 // object 146 public void run() { 147 jframe frame = new testmenu(); 148 frame.setsize(500, 400); 149 frame.setdefaultcloseoperation(jframe.exit_on_close); 150 frame.setvisible(true); 151 152 } 153 154 }); 155 156 } 157 158 }
转载于:https://www.cnblogs.com/a--q/p/6618047.html
总结
以上是凯发k8官方网为你收集整理的java--图片浏览器的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇:
 - 下一篇: