Java JPopupMenu Mac OS X. [英] Java JPopupMenu Mac OS X

查看:115
本文介绍了Java JPopupMenu Mac OS X.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Mac上使用JPopupmenu有三个问题,所有这些都可以通过随附的java
程序复制,例如netbeans java应用程序。

i have three problems with a JPopupmenu on Mac, all can be reproduced by the enclosed java program or e.g. the netbeans java application.

第一件事是Java应用程序在显示弹出菜单时不会阻塞。
因此,当我右键单击我的java应用程序打开弹出菜单时,我仍然可以
将鼠标移动到停靠区域并显示停靠栏。
在非Java应用程序(Outlook,Textwrangler,Finder ...)中,如果在这些应用程序中显示上下文菜单,则会出现

The first thing is that Java applications don't block the dock when a popup menu is shown. So when i right click in my java application to open a popup menu, i can still move the mouse over the dock area and the dock appears. In non java applications (Outlook, Textwrangler, Finder...) the dock won't appear if a context menu is shown in these applications.

有没有办法让java应用程序像'原生'OS X应用程序一样,所以
底座不会在这个上下文中显示?

Is there a way to make a java application behave like a 'native' OS X application, so the dock will not be shown in this context?

下一个问题更令人讨厌。
如果java应用程序显示上下文菜单,现在用户切换(cmd-TAB或
dock)到另一个应用程序就可以说Outlook,java应用程序的上下文菜单仍然是
在另一个应用程序窗口的顶部可见。

The next problem is more annoying. if the context menu is shown by the java application and now the user switches (cmd-TAB or by the dock) to another application lets say Outlook, the context menu of the java application is still visible on top of the other application window.

如果另一个应用程序有焦点,有没有办法隐藏java应用程序的弹出菜单?

Is there a way to hide the popup menu of the java application if another application has the focus?

最后一个问题。
让我们说应用程序在netbeans前面,现在你右键点击netbeans窗口,显示来自netbeans的
a弹出菜单,但是如果你将鼠标移到菜单项上,则没有菜单项
将突出显示。您可以通过按鼠标选择一个菜单项,但是将
鼠标移动到菜单项上它们不会突出显示。

And the last problem. Lets say an application is in front of netbeans and now you right click into the netbeans window, a popup menu from netbeans is shown, but if you move the mouse over the menu items, no menu item will be highlighted. You're able to select a menu item by pressing the mouse, but by moving the mouse over the menu items they are not highlighted.

为什么菜单项没有突出显示,有解决方法吗?

Why are the menu items not highlighted, is there a workaround?

Mac OS X 10.6.8
Java:1.6.0_35

Mac OS X 10.6.8 Java: 1.6.0_35

package popupmenu;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;

public class PopupMenuApp {
private JPopupMenu popup;

private class PopupListener extends MouseAdapter {
    @Override
    public void mousePressed(MouseEvent e) {
        maybeShowPopup(e);
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        maybeShowPopup(e);
    }

    private void maybeShowPopup(MouseEvent e) {
        if (e.isPopupTrigger()) {
            popup.show(e.getComponent(), e.getX(), e.getY());
        }
    }
}

private void start() {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            popup = new JPopupMenu();
            popup.add(new JMenuItem("A popup menu item"));
            frame.addMouseListener(new PopupListener());

            frame.setSize(300, 200);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            frame.setAlwaysOnTop(true);
        }
    });
}

public static void main(String[] args) {
    PopupMenuApp app = new PopupMenuApp();
    app.start();
}
}


推荐答案

第二个问题,添加一个WindowFocusListener可以解决这个问题:

For the second issue, adding a WindowFocusListener does the trick:

popupMenu.show(button, 0, bounds.height + 1);
frame.addWindowFocusListener(new WindowFocusListener() {
    @Override public void windowGainedFocus(WindowEvent arg0) {}
    @Override public void windowLostFocus(WindowEvent arg0) {
        popupMenu.setVisible(false);
        frame.removeWindowFocusListener(this);
    }
});

这篇关于Java JPopupMenu Mac OS X.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆