JPopupMenu  popMenu = (JPopupMenu)jMenuItem21.getParent();
JMenu menu = popMenu.getInvoker();
if(menu.getName()equals("jMenu2")){
   //then equal
}

解决方案 »

  1.   

    JPopupMenu popMenu = (JPopupMenu)jMenuItem21.getParent();
    JMenu menu = popMenu.getInvoker();
    use menu.getName() to decide whether it is jMenu2
      

  2.   

    if (jMenuItem21 instanceOf JMenu)
      

  3.   

    我试试顺便问一句,
    jMenuItem21.getParent();为什么默认返回来得类型是JPopupMenu ?
      

  4.   

    JMenuItem类继承的类中并没有JPopupMenu,为什么会返回JPopupMenu
      

  5.   

    to : soldier1002(廖运河) 
    menu.getName() 
    那里又这样的方法?to :shihb() 
    if (jMenuItem21 instanceOf JMenu)
    这只能判断jMenuItem21是否是JMenu的一个实例。并不能判断jMenuItem21是否属于JMenu.
      

  6.   

    你是什么意思?什么是属于一个Class?
      

  7.   

    JMenu jMenu2 = new JMenu("1");
      JMenuItem jMenuItem21 = new JMenuItem("2");
        jMenu2.add(jMenuItem21);如何判断jMenuItem21是否属于jMenu2里面的一项,
      

  8.   

    An implementation of a popup menu -- a small window that pops up and displays a series of choices. A JPopupMenu is used for the menu that appears when the user selects an item on the menu bar. It is also used for "pull-right" menu that appears when the selects a menu item that activates it. Finally, a JPopupMenu can also be used anywhere else you want a menu to appear. For example, when the user right-clicks in a specified area
    ******************************************************************
    I know this bc I encounter this problem before, and it cost me hours to find out why
      

  9.   

    In essence,a menu is a button with an associated JPopupMenu. When the "button" is pressed, the JPopupMenu appears. If the "button" is on the JMenuBar, the menu is a top-level window. If the "button" is another menu item, then the JPopupMenu is "pull-right" menu. 
      

  10.   

    我知道了用:
    javax.swing 
    Class MenuSelectionManager
    java.lang.Object
      |
      +-javax.swing.MenuSelectionManager MenuElement[] getSelectedPath() 
              Returns the path to the currently selected menu item