标签:menuitem cto span == bre operation cte -- 浏览器
功能:启动后选择打开文件,可以打开图片进行浏览。
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 }
标签:menuitem cto span == bre operation cte -- 浏览器
原文地址:http://www.cnblogs.com/A--Q/p/6618047.html