java实现托盘闪动消息提醒

发一个小例子供大家参考,java实现托盘闪动消息提醒,同时播放声音提醒。

1.package com.msg;  
2.  
3.import java.applet.Applet;  
4.import java.applet.AudioClip;  
5.import java.awt.AWTException;  
6.import java.awt.Image;  
7.import java.awt.MenuItem;  
8.import java.awt.PopupMenu;  
9.import java.awt.SystemTray;  
10.import java.awt.TextArea;  
11.import java.awt.TrayIcon;  
12.import java.awt.event.ActionEvent;  
13.import java.awt.event.ActionListener;  
14.import java.awt.event.MouseAdapter;  
15.import java.awt.event.MouseEvent;  
16.import java.awt.event.WindowAdapter;  
17.import java.awt.event.WindowEvent;  
18.import java.net.MalformedURLException;  
19.import java.net.URL;  
20.import java.util.Date;  
21.  
22.import javax.swing.ImageIcon;  
23.import javax.swing.JFrame;  
24.import javax.swing.SwingUtilities;  
25.import javax.swing.UIManager;  
26.import javax.swing.UnsupportedLookAndFeelException;  
27.  
28.import org.jvnet.substance.skin.SubstanceBusinessBlueSteelLookAndFeel;  
29.  
30.  
31./** 
32. *  
33. * 创建闪动的托盘图像 
34. * @author Everest 
35. * 
36. */  
37.public class BickerTray extends JFrame implements Runnable {  
38.  
39.    private static final long serialVersionUID = -3115128552716619277L;  
40.  
41.    private SystemTray sysTray;// 当前操作系统的托盘对象  
42.    private TrayIcon trayIcon;// 当前对象的托盘  
43.  
44.    private ImageIcon icon = null;  
45.    private TextArea ta = null;  
46.      
47.    private static int count = 1; //记录消息闪动的次数  
48.    private boolean flag = false; //是否有新消息  
49.    private static int times = 1; //接收消息次数  
50.  
51.    public BickerTray() {  
52.        this.createTrayIcon();// 创建托盘对象  
53.        Image image = this.getToolkit().getImage(getRes("com/img/f32.gif"));  
54.        this.setIconImage(image);  
55.        init();  
56.    }  
57.  
58.    public URL getRes(String str){  
59.         return this.getClass().getClassLoader().getResource(str);  
60.    }  
61.      
62.    /** 
63.     * 初始化窗体的方法 
64.     */  
65.    public void init() {  
66.        this.setTitle("消息盒子");  
67.        ta = new TextArea("");  
68.        ta.setEditable(false);  
69.        this.add(ta);  
70.        this.setSize(400, 400);  
71.        //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
72.        this.setLocationRelativeTo(null);  
73.        // 添加窗口最小化事件,将托盘添加到操作系统的托盘  
74.        /*this.addWindowListener(new WindowAdapter() { 
75.            public void windowIconified(WindowEvent e) { 
76.                addTrayIcon(); 
77.            } 
78.        });*/  
79.        addTrayIcon();  
80.        this.setVisible(true);  
81.    }  
82.  
83.    /** 
84.     * 添加托盘的方法 
85.     */  
86.    public void addTrayIcon() {  
87.        try {  
88.            sysTray.add(trayIcon);// 将托盘添加到操作系统的托盘  
89.            setVisible(false);    // 使得当前的窗口隐藏  
90.            new Thread(this).start();  
91.        } catch (AWTException e1) {  
92.            e1.printStackTrace();  
93.        }  
94.    }  
95.  
96.    /** 
97.     * 创建系统托盘的对象 步骤:  
98.     * 1,获得当前操作系统的托盘对象  
99.     * 2,创建弹出菜单popupMenu  
100.     * 3,创建托盘图标icon 
101.     * 4,创建系统的托盘对象trayIcon 
102.     */  
103.    public void createTrayIcon() {  
104.        sysTray = SystemTray.getSystemTray();// 获得当前操作系统的托盘对象  
105.        icon = new ImageIcon(getRes("com/img/f17.gif"));// 托盘图标  
106.        PopupMenu popupMenu = new PopupMenu();// 弹出菜单  
107.        MenuItem mi = new MenuItem("打开");  
108.        MenuItem exit = new MenuItem("退出");  
109.        popupMenu.add(mi);  
110.        popupMenu.add(exit);  
111.        // 为弹出菜单项添加事件  
112.        mi.addActionListener(new ActionListener() {  
113.            public void actionPerformed(ActionEvent e) {  
114.                ta.setText(ta.getText()+"\n==============================================\n 《通知》 今天下午4:00到大礼堂开会。 \n 第"+times+"次接收时间:"+ new Date().toLocaleString()); // 设置通知消息内容  
115.                BickerTray.this.setExtendedState(JFrame.NORMAL);  
116.                BickerTray.this.setVisible(true); // 显示窗口  
117.                BickerTray.this.toFront(); //显示窗口到最前端  
118.                flag = false;  //消息打开了  
119.                count = 0; times++;  
120.            }  
121.        });  
122.        exit.addActionListener(new ActionListener() {  
123.            public void actionPerformed(ActionEvent e) {  
124.                System.exit(0);  
125.            }  
126.        });  
127.        trayIcon = new TrayIcon(icon.getImage(), "消息盒子", popupMenu);  
128.        /** 添加鼠标监听器,当鼠标在托盘图标上双击时,默认显示窗口 */  
129.        trayIcon.addMouseListener(new MouseAdapter() {  
130.            public void mouseClicked(MouseEvent e) {  
131.                if (e.getClickCount() == 2) { // 鼠标双击  
132.                    ta.setText(ta.getText()+"\n==============================================\n 《通知》 今天下午4:00到大礼堂开会。 \n 第"+times+"次接收时间:"+ new Date().toLocaleString()); // 设置通知消息内容  
133.                    BickerTray.this.setExtendedState(JFrame.NORMAL);  
134.                    BickerTray.this.setVisible(true); // 显示窗口  
135.                    BickerTray.this.toFront();  
136.                    flag = false;  //消息打开了  
137.                    count = 0; times++;  
138.                }  
139.            }  
140.        });  
141.    }  
142.  
143.    /** 
144.     * 线程控制闪动  
145.     */  
146.    public void run() {  
147.        while (true) {  
148.            if(flag){ // 有新消息  
149.                try {  
150.                    if(count == 1){  
151.                        // 播放消息提示音  
152.                        //AudioPlayer p = new AudioPlayer(getRes("file:com/sound/Msg.wav"));  
153.                        //p.play(); p.stop();  
154.                        try {  
155.                            AudioClip p = Applet.newAudioClip(new URL("file:sound/msg.wav"));  
156.                            p.play();  
157.                        } catch (MalformedURLException e) {  
158.                            e.printStackTrace();  
159.                        }  
160.                    }  
161.                    // 闪动消息的空白时间  
162.                    this.trayIcon.setImage(new ImageIcon("").getImage());  
163.                    Thread.sleep(500);  
164.                    // 闪动消息的提示图片  
165.                    this.trayIcon.setImage(icon.getImage());  
166.                    Thread.sleep(500);  
167.                } catch (Exception e) {  
168.                    e.printStackTrace();  
169.                }  
170.                count++;  
171.            }else{ // 无消息或是消息已经打开过  
172.                this.trayIcon.setImage(icon.getImage());  
173.                try {  
174.                    Thread.sleep(20000);  
175.                    flag = true;  
176.                } catch (InterruptedException e) {  
177.                    e.printStackTrace();  
178.                }  
179.            }  
180.        }  
181.    }  
182.  
183.    /** 
184.     * @param args 
185.     */  
186.    public static void main(String[] args) {  
187.        JFrame.setDefaultLookAndFeelDecorated(true);  
188.        try {  
189.            UIManager.setLookAndFeel(new SubstanceBusinessBlueSteelLookAndFeel());  
190.        } catch (UnsupportedLookAndFeelException e) {  
191.            e.printStackTrace();  
192.        }  
193.  
194.        SwingUtilities.invokeLater(new Runnable() {  
195.            public void run() {  
196.                new BickerTray();  
197.            }  
198.        });  
199.    }  
200.  
201.}  

 

posted @ 2012-08-30 11:16  zdp072  阅读(1704)  评论(0编辑  收藏  举报