如何制作Java TrayIcon MenuItem快捷方式?

发布于 2024-11-03 16:54:26 字数 961 浏览 5 评论 0原文

我很难在 TrayIcon 弹出菜单上创建快捷方式。

以下是我的代码。

final PopupMenu popup = new PopupMenu();
final TrayIcon trayIcon = new TrayIcon(createImage("images/bulb.gif", "tray icon"));
final SystemTray tray = SystemTray.getSystemTray();

//MenuItem aboutItem = new MenuItem("About", new MenuShortcut(KeyEvent.VK_A));
MenuItem aboutItem = new MenuItem("About", new MenuShortcut(KeyEvent.VK_A, false));
Menu settingMenu = new Menu("Settings"); 
MenuItem exitItem = new MenuItem("Exit");

popup.add(aboutItem);
popup.addSeparator();
popup.add(settingMenu);
popup.addSeparator();
popup.add(exitItem);

aboutItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null,
                        "This dialog box is run from the About menu item");
            }
        });

我坚持了这一点。 为什么当我按“a”键时我的程序不起作用? 我怎样才能正确地做到这一点? 我想在弹出菜单上表达一个快捷键,例如在底部“a”字符下划线。

I have difficulty to make a shortcut on TrayIcon popup menu.

Followings are my codes.

final PopupMenu popup = new PopupMenu();
final TrayIcon trayIcon = new TrayIcon(createImage("images/bulb.gif", "tray icon"));
final SystemTray tray = SystemTray.getSystemTray();

//MenuItem aboutItem = new MenuItem("About", new MenuShortcut(KeyEvent.VK_A));
MenuItem aboutItem = new MenuItem("About", new MenuShortcut(KeyEvent.VK_A, false));
Menu settingMenu = new Menu("Settings"); 
MenuItem exitItem = new MenuItem("Exit");

popup.add(aboutItem);
popup.addSeparator();
popup.add(settingMenu);
popup.addSeparator();
popup.add(exitItem);

aboutItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null,
                        "This dialog box is run from the About menu item");
            }
        });

I stuck this point.
Why my program doesn't work when I press 'a' key?
How can I make it properly?
and I want to express a is shorcut key on popup menu such as underline bottom 'a' character.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小矜持 2024-11-10 16:54:29

这是带有 keyMnemonic 和 keyAccelerator 的正确且更正的代码,该代码将在 A

  //Check the SystemTray support
        if (!SystemTray.isSupported()) {
            System.out.println("SystemTray is not supported");
            return;
        }
        final PopupMenu popup = new PopupMenu();
        final TrayIcon trayIcon =
                new TrayIcon(createImage("images/bulb.gif", "tray icon"));
        final SystemTray tray = SystemTray.getSystemTray();

        // Create a popup menu components
        MenuItem aboutItem = new MenuItem("About");
        // Here is the change
        aboutItem.setMnemonic(KeyEvent.VK_A); 
        aboutItem .setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,
            0));

        // end change
        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 popup 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.");
            return;
        }

希望这有帮助下放置行。

Here is the proper and corrected code with keyMnemonic and keyAccelerator that will placed line under A

  //Check the SystemTray support
        if (!SystemTray.isSupported()) {
            System.out.println("SystemTray is not supported");
            return;
        }
        final PopupMenu popup = new PopupMenu();
        final TrayIcon trayIcon =
                new TrayIcon(createImage("images/bulb.gif", "tray icon"));
        final SystemTray tray = SystemTray.getSystemTray();

        // Create a popup menu components
        MenuItem aboutItem = new MenuItem("About");
        // Here is the change
        aboutItem.setMnemonic(KeyEvent.VK_A); 
        aboutItem .setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,
            0));

        // end change
        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 popup 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.");
            return;
        }

Hope this helpfull..

冰葑 2024-11-10 16:54:29

例如 http://www.javaswing.org/java-swing-popup-menu .aspx

private void showPopup(MouseEvent e) {

for example http://www.javaswing.org/java-swing-popup-menu.aspx

private void showPopup(MouseEvent e) {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文