java在任务栏上添加图标和事件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bandaotixiruiqiang/article/details/43051779
class Window extends JFrame {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private final int BUTTON_HEIGHT = 30;
    private final int WINDOW_WIDTH = 300;
    private final int WINDOW_HEIGHT = 60 + BUTTON_HEIGHT;
    private SystemTray st;
    private TrayIcon ti = null;
    private MenuItem startMenu = new MenuItem("Start");
    private MenuItem stopMenu = new MenuItem("Stop");
    private MenuItem exitMenu = new MenuItem("Exit");
    private MenuItem addByDate = new MenuItem("AddByDate");
    private PopupMenu pm;

    public MenuItem getStartMenu() {
        return startMenu;
    }

    public MenuItem getStopMenu() {
        return stopMenu;
    }

    public MenuItem getAddByDate() {
        return addByDate;
    }

    public Window(String title) {
        setTitle(title);
        init();
        addIoc();
    }

    private void addIoc() {
        if (SystemTray.isSupported()) {// 判断当前平台是否支持系统托盘
            st = SystemTray.getSystemTray();
            Image image = getImageByString("Stop.png"); // 定义托盘图标的图片
            createPopupMenu();
            ti = new TrayIcon(image, "更新数据-.xi", pm);
            ti.setImage(image);
            try {
                st.add(ti);
            } catch (AWTException ex) {
                ex.printStackTrace();
            }
        }
    }

    public Image getImageByString(String path) {
        return Toolkit.getDefaultToolkit().getImage(
                Window.class.getClassLoader().getResource(path));
    }

    public void createPopupMenu() {
        pm = new PopupMenu();
        MenuItem showInfo = new MenuItem("ShowInfo");
        startMenu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ti.setImage(getImageByString("Start.png"));
            }
        });
        stopMenu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ti.setImage(getImageByString("Stop.png"));

            }
        });
        exitMenu.addActionListener(new ActionListener() {
            
            public void actionPerformed(ActionEvent e) {
                int tag = JOptionPane.showConfirmDialog(null,
                        "是否退出?","提示",JOptionPane.YES_NO_OPTION);
                if (tag == JOptionPane.NO_OPTION){
                    
                }
                else if (tag == JOptionPane.YES_OPTION){
                    try {
                        PrintWriter pw=new PrintWriter(new FileOutputStream("log"+File.separator+"log.log", true));
                        pw.println(InfoWindows.getInfo());
                        pw.close();
                        System.exit(0);
                    } catch (FileNotFoundException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
            }
        });
        showInfo.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                InfoWindows.getWin().setVisible(true);

            }
        });

        pm.add(startMenu);

        pm.add(addByDate);
        pm.add(stopMenu);
        pm.add(showInfo);
        pm.addSeparator();
        pm.add(exitMenu);
    }

    private void init() {
        setLayout(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setVisible(false);
    }
}

猜你喜欢

转载自blog.csdn.net/bandaotixiruiqiang/article/details/43051779