java系統托盤的應用

johon 13年前發布 | 1K 次閱讀 Processing 云計算
package com.msg;   

import java.applet.Applet;   
import java.applet.AudioClip;   
import java.awt.AWTException;   
import java.awt.Image;   
import java.awt.MenuItem;   
import java.awt.PopupMenu;   
import java.awt.SystemTray;   
import java.awt.TextArea;   
import java.awt.TrayIcon;   
import java.awt.event.ActionEvent;   
import java.awt.event.ActionListener;   
import java.awt.event.MouseAdapter;   
import java.awt.event.MouseEvent;   
import java.awt.event.WindowAdapter;   
import java.awt.event.WindowEvent;   
import java.net.MalformedURLException;   
import java.net.URL;   
import java.util.Date;   

import javax.swing.ImageIcon;   
import javax.swing.JFrame;   
import javax.swing.SwingUtilities;   
import javax.swing.UIManager;   
import javax.swing.UnsupportedLookAndFeelException;   

import org.jvnet.substance.skin.SubstanceBusinessBlueSteelLookAndFeel;   


/**  
 *   
 * 創建閃動的托盤圖像  
 * @author Everest  
 *  
 */  
public class BickerTray extends JFrame implements Runnable {   

    private static final long serialVersionUID = -3115128552716619277L;   

    private SystemTray sysTray;// 當前操作系統的托盤對象   
    private TrayIcon trayIcon;// 當前對象的托盤   

    private ImageIcon icon = null;   
    private TextArea ta = null;   

    private static int count = 1; //記錄消息閃動的次數   
    private boolean flag = false; //是否有新消息   
    private static int times = 1; //接收消息次數   

    public BickerTray() {   
        this.createTrayIcon();// 創建托盤對象   
        Image image = this.getToolkit().getImage(getRes("com/img/f32.gif"));   
        this.setIconImage(image);   
        init();   
    }   

    public URL getRes(String str){   
         return this.getClass().getClassLoader().getResource(str);   
    }   

    /**  
     * 初始化窗體的方法  
     */  
    public void init() {   
        this.setTitle("消息盒子");   
        ta = new TextArea("");   
        ta.setEditable(false);   
        this.add(ta);   
        this.setSize(400, 400);   
        //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
        this.setLocationRelativeTo(null);   
        // 添加窗口最小化事件,將托盤添加到操作系統的托盤   
        /*this.addWindowListener(new WindowAdapter() {  
            public void windowIconified(WindowEvent e) {  
                addTrayIcon();  
            }  
        });*/  
        addTrayIcon();   
        this.setVisible(true);   
    }   

    /**  
     * 添加托盤的方法  
     */  
    public void addTrayIcon() {   
        try {   
            sysTray.add(trayIcon);// 將托盤添加到操作系統的托盤   
            setVisible(false);    // 使得當前的窗口隱藏   
            new Thread(this).start();   
        } catch (AWTException e1) {   
            e1.printStackTrace();   
        }   
    }   

    /**  
     * 創建系統托盤的對象 步驟:   
     * 1,獲得當前操作系統的托盤對象   
     * 2,創建彈出菜單popupMenu   
     * 3,創建托盤圖標icon  
     * 4,創建系統的托盤對象trayIcon  
     */  
    public void createTrayIcon() {   
        sysTray = SystemTray.getSystemTray();// 獲得當前操作系統的托盤對象   
        icon = new ImageIcon(getRes("com/img/f17.gif"));// 托盤圖標   
        PopupMenu popupMenu = new PopupMenu();// 彈出菜單   
        MenuItem mi = new MenuItem("打開");   
        MenuItem exit = new MenuItem("退出");   
        popupMenu.add(mi);   
        popupMenu.add(exit);   
        // 為彈出菜單項添加事件   
        mi.addActionListener(new ActionListener() {   
            public void actionPerformed(ActionEvent e) {   
                ta.setText(ta.getText()+"\n==============================================\n 《通知》 今天下午4:00到大禮堂開會。 \n 第"+times+"次接收時間:"+ new Date().toLocaleString()); // 設置通知消息內容   
                BickerTray.this.setExtendedState(JFrame.NORMAL);   
                BickerTray.this.setVisible(true); // 顯示窗口   
                BickerTray.this.toFront(); //顯示窗口到最前端   
                flag = false;  //消息打開了   
                count = 0; times++;   
            }   
        });   
        exit.addActionListener(new ActionListener() {   
            public void actionPerformed(ActionEvent e) {   
                System.exit(0);   
            }   
        });   
        trayIcon = new TrayIcon(icon.getImage(), "消息盒子", popupMenu);   
        /** 添加鼠標監聽器,當鼠標在托盤圖標上雙擊時,默認顯示窗口 */  
        trayIcon.addMouseListener(new MouseAdapter() {   
            public void mouseClicked(MouseEvent e) {   
                if (e.getClickCount() == 2) { // 鼠標雙擊   
                    ta.setText(ta.getText()+"\n==============================================\n 《通知》 今天下午4:00到大禮堂開會。 \n 第"+times+"次接收時間:"+ new Date().toLocaleString()); // 設置通知消息內容   
                    BickerTray.this.setExtendedState(JFrame.NORMAL);   
                    BickerTray.this.setVisible(true); // 顯示窗口   
                    BickerTray.this.toFront();   
                    flag = false;  //消息打開了   
                    count = 0; times++;   
                }   
            }   
        });   
    }   

    /**  
     * 線程控制閃動   
     */  
    public void run() {   
        while (true) {   
            if(flag){ // 有新消息   
                try {   
                    if(count == 1){   
                        // 播放消息提示音   
                        //AudioPlayer p = new AudioPlayer(getRes("file:com/sound/Msg.wav"));   
                        //p.play(); p.stop();   
                        try {   
                            AudioClip p = Applet.newAudioClip(new URL("file:sound/msg.wav"));   
                            p.play();   
                        } catch (MalformedURLException e) {   
                            e.printStackTrace();   
                        }   
                    }   
                    // 閃動消息的空白時間   
                    this.trayIcon.setImage(new ImageIcon("").getImage());   
                    Thread.sleep(500);   
                    // 閃動消息的提示圖片   
                    this.trayIcon.setImage(icon.getImage());   
                    Thread.sleep(500);   
                } catch (Exception e) {   
                    e.printStackTrace();   
                }   
                count++;   
            }else{ // 無消息或是消息已經打開過   
                this.trayIcon.setImage(icon.getImage());   
                try {   
                    Thread.sleep(20000);   
                    flag = true;   
                } catch (InterruptedException e) {   
                    e.printStackTrace();   
                }   
            }   
        }   
    }   

    /**  
     * @param args  
     */  
    public static void main(String[] args) {   
        JFrame.setDefaultLookAndFeelDecorated(true);   
        try {   
            UIManager.setLookAndFeel(new SubstanceBusinessBlueSteelLookAndFeel());   
        } catch (UnsupportedLookAndFeelException e) {   
            e.printStackTrace();   
        }   

        SwingUtilities.invokeLater(new Runnable() {   
            public void run() {   
                new BickerTray();   
            }   
        });   
    }   

}  

 本文由用戶 johon 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!