先來看看效果:
這是一個Application運行效果

然后我突發奇想,我之前做過一個Application叫做:java開發_java小程序_郵死你(yousini)_源碼下載
為什么不去修改它的源代碼,把這個功能實現在一個桌面應用程序中去呢?
就這樣有一個java小程序就這樣誕生啦....

==========================================================
代碼部分:
==========================================================
上面的Application代碼:
1 /** 2 * 3 */ 4 package com.b510; 5 6 import java.io.File; 7 import java.util.Date; 8 9 public class ThreadDemo implements Runnable { 10 // 要統計的磁盤路徑 11 private String path; 12 13 // 構造方法 14 public ThreadDemo(String path) { 15 this.path = path; 16 } 17 18 // 主方法 19 public static void main(String[] args) { 20 // 得到根目錄 21 File[] root = File.listRoots(); 22 System.out.println("所有目錄:"); 23 for (int i = 0; i < root.length; i++) { 24 System.out.println(root[i].getAbsolutePath()); 25 } 26 System.out.println("====================================="); 27 for (int i = 0; i < root.length; i++) { 28 System.out.println("開始處理:" + root[i].getAbsolutePath() + " 目錄..."); 29 // 創建線程對象 30 ThreadDemo td = new ThreadDemo(root[i].getAbsolutePath()); 31 new Thread(td).start(); 32 } 33 } 34 35 // 重寫run方法 36 public void run() { 37 long start = new Date().getTime(); 38 int num = countFile(path); 39 long end = new Date().getTime(); 40 System.out.println(path + "統計共有" + num + "個文件!共耗時:[" + (end - start) + "]ms"); 41 } 42 43 // 統計文件數目的方法 44 public int countFile(String path) { 45 int count = 0; 46 File file = new File(path); 47 // 得到該目錄下的所有文件 48 File[] subFile = file.listFiles(); 49 // 如果該目錄為空或 50 if (null == subFile || subFile.length == 0) { 51 return count; 52 } 53 for (int i = 0; i < subFile.length; i++) { 54 if (subFile[i].isDirectory()) { 55 // System.out.println(subFile[i].getAbsolutePath()); 56 count += countFile(subFile[i].getAbsolutePath()); 57 } else if (subFile[i].isFile()) { 58 if (getPostfix(subFile[i].getName()).equals("gif")) { 59 System.out.println("文件名稱:" + subFile[i].getAbsolutePath()); 60 } 61 count++; 62 } 63 } 64 return count; 65 } 66 67 /** 68 * 獲取inputFilePath的后綴名,如:"e:/test.pptx"的后綴名為:"pptx"<br> 69 * 70 * @param inputFilePath 71 * @return 72 */ 73 public String getPostfix(String inputFilePath) { 74 return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1); 75 } 76 77 }
下面的桌面應用程序代碼:
主要參考:java開發_java小程序_郵死你(yousini)_源碼下載
我把修改的類的代碼貼出來:(其實就修改了兩個類 ^_^)
/SearchFiles/src/com/b510/sendmail/main/SendMailMain.java
1 package com.b510.sendmail.main; 2 3 import com.b510.sendmail.ui.SendMailUI; 4 5 /** 6 * @author Hongten 7 * 8 * @time 2012-4-4 2012 9 */ 10 public class SendMailMain { 11 public static void main(String[] args) { 12 SendMailUI sendMailUI = new SendMailUI("MySearchFileUtil v1.0"); 13 } 14 }
/SearchFiles/src/com/b510/sendmail/ui/SendMailUI.java
1 package com.b510.sendmail.ui; 2 3 import java.awt.Graphics; 4 import java.awt.event.ActionEvent; 5 import java.awt.event.ActionListener; 6 import java.awt.event.WindowAdapter; 7 import java.awt.event.WindowEvent; 8 9 import javax.swing.ImageIcon; 10 import javax.swing.JFrame; 11 import javax.swing.JOptionPane; 12 import javax.swing.JPanel; 13 14 import com.b510.sendmail.utils.AboutSendmail; 15 16 /** 17 * 18 * @author Hongten 19 */ 20 public class SendMailUI extends JFrame implements ActionListener { 21 22 public static javax.swing.JTextArea infoShow; 23 private javax.swing.JScrollPane jScrollPane1; 24 private javax.swing.JPanel mainPanel; 25 private javax.swing.JMenuBar menuBar; 26 private javax.swing.JButton send; 27 private javax.swing.JProgressBar showTime; 28 private javax.swing.JTextField targetmail; 29 javax.swing.JMenu fileMenu; 30 javax.swing.JMenuItem exitMenuItem; 31 javax.swing.JMenu helpMenu; 32 javax.swing.JMenuItem aboutMenuItem; 33 private static boolean flag; 34 private static int number = 266; 35 private int i = 0; 36 private AboutSendmail aboutSendmail; 37 /** 38 * 定義圖盤圖盤標志 39 */ 40 public boolean iconed = false; 41 /** 定義托盤 */ 42 MyTray myTray; 43 44 /** 45 * 版本號 46 */ 47 private static final long serialVersionUID = -6601825053136983041L; 48 49 public SendMailUI(String title) { 50 this.setTitle(title); 51 init(); 52 } 53 54 /** 55 * 主界面初始化 56 */ 57 public void init() { 58 59 mainPanel = new JPanel() { 60 private static final long serialVersionUID = 1L; 61 62 protected void paintComponent(Graphics g) { 63 ImageIcon icon = new ImageIcon("src/resources/mail_bg.png"); 64 g.drawImage(icon.getImage(), 0, 0, 700, 430, null); 65 } 66 }; 67 68 targetmail = new javax.swing.JTextField(); 69 send = new javax.swing.JButton(); 70 jScrollPane1 = new javax.swing.JScrollPane(); 71 infoShow = new javax.swing.JTextArea(); 72 menuBar = new javax.swing.JMenuBar(); 73 fileMenu = new javax.swing.JMenu(); 74 exitMenuItem = new javax.swing.JMenuItem(); 75 helpMenu = new javax.swing.JMenu(); 76 aboutMenuItem = new javax.swing.JMenuItem(); 77 // 設置為0到180,即180s,3分鍾 78 showTime = new javax.swing.JProgressBar(0, number); 79 aboutSendmail = new AboutSendmail("關於軟件"); 80 aboutSendmail.setVisible(false); 81 82 // 是否在進度條上顯示字符 83 showTime.setStringPainted(true); 84 85 mainPanel.setName("mainPanel"); // NOI18N 86 87 targetmail.setName("targetmail"); // NOI18N 88 // targetmail.setText("hongtenzone@foxmail.com"); 89 90 send.setText("search"); // NOI18N 91 send.setName("search"); // NOI18N 92 send.setEnabled(false); 93 send.addActionListener(this); 94 95 jScrollPane1.setName("jScrollPane1"); // NOI18N 96 97 infoShow.setColumns(20); 98 infoShow.setRows(5); 99 infoShow.setName("infoShow"); // NOI18N 100 jScrollPane1.setViewportView(infoShow); 101 // 初始化布局 102 initComponent(); 103 104 menuBar.setName("menuBar"); // NOI18N 105 106 fileMenu.setName("fileMenu"); // NOI18N 107 fileMenu.setText("file"); 108 109 exitMenuItem.setText("exit"); 110 fileMenu.add(exitMenuItem); 111 exitMenuItem.addActionListener(this); 112 113 menuBar.add(fileMenu); 114 115 helpMenu.setText("help"); // NOI18N 116 helpMenu.setName("helpMenu"); // NOI18N 117 118 aboutMenuItem.setText("about"); 119 helpMenu.add(aboutMenuItem); 120 aboutMenuItem.addActionListener(this); 121 122 menuBar.add(helpMenu); 123 124 this.add(mainPanel); 125 setJMenuBar(menuBar); 126 127 this.setVisible(true); 128 this.setSize(700, 485); 129 // 啟動進度條記時監聽器 130 timeCardListener(); 131 // 啟動郵箱輸入框監聽器 132 myListener(); 133 // this.pack(); 134 this.setLocationRelativeTo(null); 135 this.setResizable(false); 136 // this.setLocation(470, 250); 137 // this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 138 // 當點擊"-"最小化按鈕時,系統會最小化到托盤 139 addWindowListener(new WindowAdapter() { 140 public void windowIconified(WindowEvent e) { 141 iconed = true; 142 setVisible(false); 143 } 144 145 public void windowClosing(WindowEvent e) { 146 // 當點擊"X"關閉窗口按鈕時,會詢問用戶是否要最小化到托盤 147 // 是,表示最小化到托盤,否,表示退出 148 int option = JOptionPane.showConfirmDialog(SendMailUI.this, "是否最小化到托盤?", "提示:", JOptionPane.YES_NO_OPTION); 149 if (option == JOptionPane.YES_OPTION) { 150 iconed = true; 151 SendMailUI.this.setVisible(false); 152 } else { 153 System.exit(0); 154 } 155 } 156 }); 157 // 初始化自定義托盤 158 myTray = new MyTray(SendMailUI.this); 159 160 } 161 162 /** 163 * 布局文件,沒有必要去管他 164 */ 165 private void initComponent() { 166 javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel); 167 mainPanel.setLayout(mainPanelLayout); 168 mainPanelLayout.setHorizontalGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( 169 mainPanelLayout 170 .createSequentialGroup() 171 .addGap(52, 54, 54) 172 .addGroup( 173 mainPanelLayout 174 .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 175 .addGroup( 176 mainPanelLayout.createSequentialGroup().addComponent(targetmail, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE) 177 .addGap(34, 34, 37).addComponent(send)) 178 .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 620, javax.swing.GroupLayout.PREFERRED_SIZE) 179 .addGroup( 180 mainPanelLayout.createSequentialGroup().addGap(463, 463, 463) 181 .addComponent(showTime, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) 182 .addContainerGap(30, Short.MAX_VALUE))); 183 mainPanelLayout.setVerticalGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( 184 mainPanelLayout 185 .createSequentialGroup() 186 .addContainerGap() 187 .addGroup( 188 mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(send) 189 .addComponent(targetmail, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)).addGap(30, 43, 43) 190 .addComponent(showTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 191 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 192 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 313, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(15, 15, 15))); 193 } 194 195 // 進度條記時 196 public void timeCard(final boolean flag) { 197 new Thread(new Runnable() {// 設置一個線程 198 public void run() { 199 if (flag) { 200 while (i < number) { 201 try { 202 Thread.sleep((long) (1000 * 7.7)); 203 } catch (Exception e) { 204 e.printStackTrace(); 205 } 206 showTime.setValue(i++); 207 } 208 } else { 209 i = 0; 210 showTime.setValue(0); 211 212 } 213 } 214 }).start(); 215 } 216 217 // 進度條記時 218 public void timeCardListener() { 219 new Thread(new Runnable() {// 設置一個線程 220 public void run() { 221 while (true) { 222 try { 223 Thread.sleep((long) (1000 * 7.7)); 224 } catch (Exception e) { 225 e.printStackTrace(); 226 } 227 timeCard(flag); 228 } 229 } 230 }).start(); 231 } 232 233 @Override 234 public void actionPerformed(ActionEvent e) { 235 if (e.getSource() == send) { 236 infoShow.append("請耐心等待...\n"); 237 new SendMail().sendMailListener(targetmail.getText()); 238 } 239 // 退出程序 240 if (e.getSource() == exitMenuItem) { 241 System.exit(0); 242 } 243 if (e.getSource() == aboutMenuItem) { 244 aboutSendmail.setVisible(true); 245 } 246 } 247 248 class SendMail { 249 public void sendMailListener(final String targetmail) { 250 new Thread(new Runnable() {// 設置一個線程 251 public void run() { 252 while (true) { 253 try { 254 // 線程處於睡眠的時候,flag=true; 255 Thread.sleep(1); 256 } catch (Exception e) { 257 e.printStackTrace(); 258 } 259 MySearchUtil.beginSearch(targetmail); 260 try { 261 // 等待timeCardListener監聽器監聽時間 262 Thread.sleep(1000000000); 263 } catch (Exception e) { 264 } 265 } 266 } 267 268 }).start(); 269 } 270 } 271 272 // 郵箱輸入框是否正確填寫 273 public void changedUpdate() { 274 String mail = targetmail.getText(); 275 send.setEnabled(true); 276 if (mail.equals("")) { 277 send.setEnabled(false); 278 } else if (mail.length() > 0) { 279 // 輸入內容不為空,且是能夠匹配郵箱,那么設置send可用 280 send.setEnabled(true); 281 } else { 282 send.setEnabled(true); 283 } 284 } 285 286 /** 287 * 郵箱是否填寫完整監聽器 288 */ 289 public void myListener() { 290 new Thread(new Runnable() {// 設置一個線程 291 public void run() { 292 while (true) { 293 try { 294 Thread.sleep(500); 295 } catch (Exception e) { 296 e.printStackTrace(); 297 } 298 changedUpdate();// 填寫郵箱 299 } 300 } 301 }).start(); 302 } 303 }
I'm Hongten

