java - 如何从 Java 中的系统托盘弹出 JFrame

标签 java windows swing jframe system-tray

我可以用 Java 创建系统托盘应用程序,但我在定位方面遇到了问题。该程序只需要处理一些输入/输出,所以我希望它易于访问。

我的问题是,当我单击我的应用程序的系统托盘图标时,我如何才能将它的位置优雅地设置在系统托盘上方?要求是无论显示设置(分辨率、多显示器等)和任务栏位置如何,它都会执行此操作。有没有办法告诉它在托盘附近打开,而不是完全定位它?

我希望它完全按照 Windows 中的“网络”设置按钮执行操作。类似于以下内容:

Network System Tray Image

这在 Java 中可行吗?

最佳答案

正如 Vulcan 所指出的,是的,这是可能的。

此外,还可以考虑放置弹出窗口的位置(与任务图标所在的位置相关)

Show me the popup

public class TestTrayIcon {

    protected static final PopupFrame POPUP_FRAME = new PopupFrame();

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {

                try {
                    TrayIcon trayIcon = new TrayIcon(ImageIO.read(new File("D:/DevWork/common/icons/iconex_v_bundle_png/iconexperience/v_collections_png/basic_foundation/16x16/plain/about.png")));
                    trayIcon.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {

                            Point pos = e.getLocationOnScreen();
                            Rectangle screen = getScreenBoundsAt(pos);

                            if (pos.x + POPUP_FRAME.getWidth() > screen.x + screen.width) {
                                pos.x = screen.x + screen.width - POPUP_FRAME.getWidth();
                            }
                            if (pos.x < screen.x) {
                                pos.x = screen.x;
                            }

                            if (pos.y + POPUP_FRAME.getHeight() > screen.y + screen.height) {
                                pos.y = screen.y + screen.height - POPUP_FRAME.getHeight();
                            }
                            if (pos.y < screen.y) {
                                pos.y = screen.y;
                            }

                            POPUP_FRAME.setLocation(pos);
                            POPUP_FRAME.setVisible(true);

                        }
                    });
                    SystemTray.getSystemTray().add(trayIcon);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    public static class PopupFrame extends JFrame {

        public PopupFrame() throws HeadlessException {
            setLayout(new BorderLayout());
            add(new JLabel("Hello world"));
            pack();
        }
    }

    public static Rectangle getScreenBoundsAt(Point pos) {
        GraphicsDevice gd = getGraphicsDeviceAt(pos);
        Rectangle bounds = null;

        if (gd != null) {
            bounds = gd.getDefaultConfiguration().getBounds();
            Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gd.getDefaultConfiguration());

            bounds.x += insets.left;
            bounds.y += insets.top;
            bounds.width -= (insets.left + insets.right);
            bounds.height -= (insets.top + insets.bottom);
        }
        return bounds;
    }

    public static GraphicsDevice getGraphicsDeviceAt(Point pos) {
        GraphicsDevice device = null;

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice lstGDs[] = ge.getScreenDevices();

        ArrayList<GraphicsDevice> lstDevices = new ArrayList<GraphicsDevice>(lstGDs.length);

        for (GraphicsDevice gd : lstGDs) {
            GraphicsConfiguration gc = gd.getDefaultConfiguration();
            Rectangle screenBounds = gc.getBounds();

            if (screenBounds.contains(pos)) {
                lstDevices.add(gd);
            }
        }

        if (lstDevices.size() == 1) {
            device = lstDevices.get(0);
        }
        return device;
    }
}

关于java - 如何从 Java 中的系统托盘弹出 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12133273/

相关文章:

java - 日期应该如何发布到java?

ruby - 在 Windows 上的 ruby​​ 中获取 SSL 错误

java - 根据多个按键更改 jtable 行选择

c# - C# 和远程桌面问题的屏幕截图

windows - Ruby 可以用来开发简单的 Windows 应用程序吗?

Java - 使用 SpringLayout 的 JScrollPane View 布局

java - JTable 排序表的选择间隔问题

JAVA - 优先级队列比较双降序

java - 如何使用添加了 toString 的 arraylist 访问类中的成员

java - 如何让 Spring Boot 应用程序在数据源是否可用的情况下运行