增加系统托盘(任务栏右下角箭头展开小图标)

/**
     * 增加系统托盘
     */
    private void addTray() {
        // 创建系统托盘
        // 判断系统是否支持系统托盘
        if (SystemTray.isSupported()) {

            // 创建系统托盘
            SystemTray tray = SystemTray.getSystemTray();
            // 创建弹出菜单
            PopupMenu popup = new PopupMenu();
            MenuItem exitItem = new MenuItem("exit");
            exitItem.addActionListener(e -> System.exit(0));
            popup.add(exitItem);


            final Image image1 = Toolkit.getDefaultToolkit().getImage("app-daemon.png");
            // 创建trayIcon
            TrayIcon trayIcon = new TrayIcon(image1, "UhanApp", popup);
            trayIcon.setImageAutoSize(true);
            try {
                tray.add(trayIcon);
            } catch (AWTException e1) {
                e1.printStackTrace();
            }
        }
    }
posted @ 2025-01-20 15:25  狗狗没有坏心眼  阅读(33)  评论(0)    收藏  举报