如何在Java中从System Tray中popupJFrame

我能够用Java创build一个系统托盘应用程序,但是我在定位时遇到了麻烦。 该程序只需要处理一些input/输出,所以我希望它很容易访问。

我的问题是,当我单击我的应用程序的系统托盘图标时,我怎样才能将它的位置放在系统托盘的上方? 要求是这样做,无论显示设置(分辨率,多显示器等)和任务栏的位置。 有没有办法告诉它在托盘附近打开,而不是放在托盘上?

我希望它能够完成Windows中的“networking”设置button。 类似于以下内容:

网络系统托盘图像

这在Java中可能吗?

Solutions Collecting From Web of "如何在Java中从System Tray中popupJFrame"

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

另外,考虑在哪里放置弹出窗口(与任务图标的位置相关)是非常可能的,

让我看看弹出

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.addMouselistner(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; } } 

对的,这是可能的。

要设置相对于屏幕右下角的窗口位置,可以获得图形环境的最大窗口范围,然后执行简单的数学运算。

 Rectangle screen = GraphicsEnvironment.getLocalGraphicsEnvironment() .getMaximumWindowBounds(); String os = System.getProperty("os.name"); if (os.contains("Windows")) { setLocation(screen.width - windowSize.width - 6, screen.height - windowSize.height - 6); } else if (os.contains("Mac")) { setLocation(screen.width - windowSize.width + 6, 6); } 

在宽度和高度上都占用了窗口和任务栏之间的小间隙,并且易于定制。

但是,使用此方法注意位于顶部或左侧的Windows任务栏在窗口的右侧/底部仍将具有任务栏大小的间隙。 我不相信可以从Java API单独找到任务栏的方向。