日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

Java--图片浏览器

發布時間:2025/7/25 HTML 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的Java--图片浏览器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。