⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trayiconutil.java

📁 java实现windows系统托盘(java)
💻 JAVA
字号:
import java.awt.*;import java.awt.event.*;import java.awt.image.*;import javax.swing.*;import java.io.*;/** * <p>Title: TrayIcon</p> * <p>Description: TrayIconUtil</p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a> * @version 1.0 */public class TrayIconUtil{    private JPopupMenu rightPopup;    private JWindow popupWindow;    private JFrame mainFrame;    private static TrayIconUtil staticInstance;    private TrayIconUtil(){}    public static TrayIconUtil getDefaultTrayIconUtil(){        if(staticInstance == null){            staticInstance = new TrayIconUtil();            staticInstance.initTrayIcon(staticInstance,"JavaTrayIconApp",1000);        }        return staticInstance;    }    private void setMainJFrame(final JFrame frame){        this.mainFrame=frame;        frame.addWindowListener(new WindowAdapter() {            public void windowClosing(WindowEvent e) {                closeTrayIcon();            }            public void windowIconified(WindowEvent e) {                frame.setVisible(false);            }            public void windowDeactivated(WindowEvent e) {                if(rightPopup!=null)                    rightPopup.setVisible(false);            }        });    }    public void setTrayIconJPopupMenu(JPopupMenu popup){        this.rightPopup=popup;        rightPopup.show(getPopupWindow(),0,0);        rightPopup.setVisible(false);    }    private void processTrayIconMouseEvent(int button,int x,int y){        if(button == -1)return;//mouse over.        if(button == 0 ||button ==5 ||button == 7){            if(rightPopup != null && button == 5){                rightPopup.show(getPopupWindow(),x,y);                rightPopup.requestFocus();                rightPopup.repaint();            }        }else{            if(button == 3){                if(rightPopup!=null)                    rightPopup.setVisible(false);                if(!mainFrame.isVisible()){                    mainFrame.setVisible(true);                    mainFrame.toFront();                    mainFrame.requestFocus();                    if(mainFrame.isVisible()){                        if(mainFrame.getExtendedState()==JFrame.ICONIFIED){                            mainFrame.setExtendedState(JFrame.NORMAL);                        }                    }                }else{                    mainFrame.setVisible(false);                }            }        }    }    private Window getPopupWindow(){        if(popupWindow == null){            popupWindow = new JWindow();            popupWindow.setBounds(0,0,1,1);            popupWindow.show();        }        return popupWindow;    }    private boolean isFirst=true;    public void initTrayIcon(String tip,File iconfile,JFrame frame,JPopupMenu popup){        if(isFirst){            if(iconfile!=null){                ImageIcon icon=new ImageIcon(iconfile.getAbsolutePath());                int w=icon.getIconWidth(),h=icon.getIconHeight();                Image image=icon.getImage();                try {                    int[] pixels = new int[w * h];                    PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);                    pg.grabPixels();                    if ((pg.getStatus() & ImageObserver.ABORT) != 0) {                        throw new Exception("Error loading icon image");                    }                    setTrayIconData(w, h, pixels);                } catch (Exception ex) {                    ex.printStackTrace();                }            }            if(tip!=null){                setTrayIconTip(tip);            }            setTrayIconJPopupMenu(popup);            setMainJFrame(frame);            isFirst=false;        }    }    private native void initTrayIcon(TrayIconUtil instance,String appName,int id);    private native void setTrayIconTip(String tip);    private native void setTrayIconData(int w, int h, int pixels[]);    public native void showTrayIcon();    public native void closeTrayIcon();    static{        System.loadLibrary("TrayIcon");    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -