先来看看效果:
这是一个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 }