栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

当我用Java左键单击一个TrayIcon时,如何显示PopupMenu?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

当我用Java左键单击一个TrayIcon时,如何显示PopupMenu?

基本上,在您的鼠标侦听器中,您需要确定按下了哪个按钮(以及可选的按下次数)。

关键代码段是这样的…

if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 1) { ... }

我还添加了一些其他代码,以确保弹出窗口不会覆盖任务栏,并且不会显示在屏幕的可视区域内(这是我的固有选择;)。

public class TestTrayIcon02 {    public static void main(String[] args) {        EventQueue.invokeLater(new Runnable() { @Override public void run() {     try {         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());     } catch (Exception ex) {     }     try {         final TrayIcon ti = new TrayIcon(ImageIO.read(getClass().getResource("/Smiley.png")), "Have a nice day");         final JPopupMenu popup = new JPopupMenu();         JMenuItem mi = new JMenuItem("Get me some");         mi.addActionListener(new ActionListener() {  @Override  public void actionPerformed(ActionEvent e) {      SystemTray.getSystemTray().remove(ti);      System.exit(0);  }         });         popup.add(mi);         ti.addMouseListener(new MouseAdapter() {  @Override  public void mouseClicked(MouseEvent e) {      if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 1) {          Rectangle bounds = getSafeScreenBounds(e.getPoint());          Point point = e.getPoint();          int x = point.x;          int y = point.y;          if (y < bounds.y) {   y = bounds.y;          } else if (y > bounds.y + bounds.height) {   y = bounds.y + bounds.height;          }          if (x < bounds.x) {   x = bounds.x;          } else if (x > bounds.x + bounds.width) {   x = bounds.x + bounds.width;          }          if (x + popup.getPreferredSize().width > bounds.x + bounds.width) {   x = (bounds.x + bounds.width) - popup.getPreferredSize().width;          }          if (y + popup.getPreferredSize().height > bounds.y + bounds.height) {   y = (bounds.y + bounds.height) - popup.getPreferredSize().height;          }          popup.setLocation(x, y);          popup.setVisible(true);      }  }         });         SystemTray.getSystemTray().add(ti);     } catch (Exception ex) {         ex.printStackTrace();     } }        });    }    public static Rectangle getSafeScreenBounds(Point pos) {        Rectangle bounds = getScreenBoundsAt(pos);        Insets insets = getScreenInsetsAt(pos);        bounds.x += insets.left;        bounds.y += insets.top;        bounds.width -= (insets.left + insets.right);        bounds.height -= (insets.top + insets.bottom);        return bounds;    }    public static Insets getScreenInsetsAt(Point pos) {        GraphicsDevice gd = getGraphicsDeviceAt(pos);        Insets insets = null;        if (gd != null) { insets = Toolkit.getDefaultToolkit().getScreenInsets(gd.getDefaultConfiguration());        }        return insets;    }    public static Rectangle getScreenBoundsAt(Point pos) {        GraphicsDevice gd = getGraphicsDeviceAt(pos);        Rectangle bounds = null;        if (gd != null) { bounds = gd.getDefaultConfiguration().getBounds();        }        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() > 0) { device = lstDevices.get(0);        } else { device = ge.getDefaultScreenDevice();        }        return device;    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/569679.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号