使用java在任务栏图标上打开弹出窗口(菜单),左键单击
2022-09-04 08:18:48
我正在使用Java中的任务托盘图标,我喜欢使用左键单击打开弹出菜单,就像我右键单击时打开一样,请帮助我快速响应。
提前致谢...
这是右键点击需要显示相同弹出窗口的代码左键点击...不要忘记在“src/img”文件夹中放置任何名为“titleImg.jpg”的图像
只需运行此...这是一个工作示例,但我必须使用左键单击显示相同的弹出窗口
我已经检查了鼠标监听器,它听左键点击托盘图标,但如何使用该???
package com.abc.dao;
import java.awt.AWTException;
import java.awt.CheckboxMenuItem;
import java.awt.Menu;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
public class MyTaskTray {
public static void main(String arg[]){
//Check the SystemTray is supported
if (!SystemTray.isSupported()) {
System.out.println("SystemTray is not supported");
return;
}
final PopupMenu popup = new PopupMenu();
final TrayIcon trayIcon =
new TrayIcon(Toolkit.getDefaultToolkit().getImage(new java.io.File("").getAbsolutePath()+"/bin/img/titleImg.jpg"), "Library Drop");
final SystemTray tray = SystemTray.getSystemTray();
// Create a pop-up menu components
MenuItem aboutItem = new MenuItem("About");
CheckboxMenuItem cb1 = new CheckboxMenuItem("Set auto size");
CheckboxMenuItem cb2 = new CheckboxMenuItem("Set tooltip");
Menu displayMenu = new Menu("Display");
MenuItem errorItem = new MenuItem("Error");
MenuItem warningItem = new MenuItem("Warning");
MenuItem infoItem = new MenuItem("Info");
MenuItem noneItem = new MenuItem("None");
MenuItem exitItem = new MenuItem("Exit");
//Add components to pop-up menu
popup.add(aboutItem);
popup.addSeparator();
popup.add(cb1);
popup.add(cb2);
popup.addSeparator();
popup.add(displayMenu);
displayMenu.add(errorItem);
displayMenu.add(warningItem);
displayMenu.add(infoItem);
displayMenu.add(noneItem);
popup.add(exitItem);
trayIcon.setPopupMenu(popup);
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.out.println("TrayIcon could not be added.");
}
}
}