项目结构:
运行效果:
如果你感兴趣,请不要那我的邮箱做测试!!!!
==========================================================
下面是代码部分
==========================================================
/SendMail/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("send E-Mail"); 13 } 14 }
/SendMail/src/com/b510/sendmail/ui/MyTray.java
1 package com.b510.sendmail.ui;
2
3 import java.awt.AWTException;
4 import java.awt.Image;
5 import java.awt.MenuItem;
6 import java.awt.PopupMenu;
7 import java.awt.SystemTray;
8 import java.awt.TrayIcon;
9 import java.awt.event.ActionEvent;
10 import java.awt.event.ActionListener;
11 import java.awt.event.MouseEvent;
12 import java.awt.event.MouseListener;
13
14 import javax.swing.ImageIcon;
15 import javax.swing.JFrame;
16
17 public class MyTray implements ActionListener, MouseListener {
18 private Image icon;// 图标
19 private TrayIcon trayIcon;
20 private SystemTray systemTray;// 系统托盘
21
22 private SendMailUI sendMailUI; // 托盘所属主窗体
23 private PopupMenu pop = new PopupMenu(); // 弹出菜单
24 // 菜单选项
25 /** 还原 */
26 private MenuItem open = new MenuItem("Restore");
27 /** 退出*/
28 private MenuItem exit =new MenuItem("Exit");
29 public MyTray(SendMailUI sendMailUI) {
30 this.sendMailUI = sendMailUI;
31 // 得到托盘的图标
32 icon = new ImageIcon(this.getClass().getClassLoader().getResource(
33 "resources/mytray.png")).getImage();
34
35 if (SystemTray.isSupported()) {
36 systemTray = SystemTray.getSystemTray();
37 // 设置鼠标经过图标时,显示的内容
38 trayIcon = new TrayIcon(icon, "YouSiNi", pop);
39 pop.add(open);
40 pop.add(exit);
41 // 添加系统托盘
42 try {
43 systemTray.add(trayIcon);
44 } catch (AWTException e1) {
45 e1.printStackTrace();
46 trayIcon.addMouseListener(this);
47 }
48 }
49 trayIcon.addMouseListener(this);
50 exit.addActionListener(this);
51 open.addActionListener(this);
52 }
53
54 @Override
55 public void actionPerformed(ActionEvent e) {
56 if(e.getSource()==exit){
57 //退出系统
58 System.exit(0);
59 }else if (e.getSource() == open) {
60 // 单点击菜单中的"还原"选项时,还原窗口
61 sendMailUI.iconed = false;
62 friendListSet(true);
63 }
64 }
65
66 @Override
67 public void mouseClicked(MouseEvent e) {
68 // 但鼠标点击一次的时候,进行弹出窗口
69 if (e.getClickCount() == 1 && e.getButton() != MouseEvent.BUTTON3) {
70 if (!sendMailUI.isVisible()) {
71 friendListSet(true);
72 } else {
73 friendListSet(false);
74 }
75 }
76 // 但鼠标点击两次的时候,进行弹出窗口
77 // 如果窗口有显示,则隐藏窗口,否则显示窗口
78 if (e.getClickCount() == 2 && e.getButton() != MouseEvent.BUTTON3) {
79 if (!sendMailUI.isVisible()) {
80 friendListSet(true);
81 } else {
82 friendListSet(false);
83 }
84 }
85 }
86
87 /**
88 * 设置friendList的可见性
89 */