gpt4 book ai didi

java - 当我在 Java 中左键单击 TrayIcon 时,如何让 PopupMenu 显示出来?

转载 作者:行者123 更新时间:2023-11-29 10:17:08 25 4
gpt4 key购买 nike

目前,当我右键单击系统托盘中的 TrayIcon 时,将显示 PopupMenu。但是,当我左键单击 TrayIcon 时,我希望它执行相同的操作。

我想我可以通过在 TrayIcon 上使用 mouseListener 来完成此操作,但我不知道在 mouseClicked 事件中调用什么方法来实现所需的结果。

icon = new TrayIcon(img, tooltip, popup);

icon.addMouseListener(
new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
popup.setEnabled(true);
}
});

当我左键单击 TrayIcon 时,使用 setEnabled() 方法不会显示弹出菜单。它实际上没有明显的效果。我想知道我应该在 mouseClicked() 主体中使用什么方法,以便在左键单击时显示弹出窗口。

最佳答案

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

关键的一段代码是...

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;

}
}

关于java - 当我在 Java 中左键单击 TrayIcon 时,如何让 PopupMenu 显示出来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14670516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com