2014-07-12 101 views
-5

我怎樣才能檢索到當前選擇的菜單或菜單項上點擊它和隨後的路徑將在控制檯上進行打印時。在這段代碼中,我完成了4層菜單和子菜單。並且想要打印點擊時所選菜單和子菜單的路徑。我爲這個程序使用了swing概念。請幫忙。提前致謝。如何獲得當前選定的菜單或菜單項

import java.awt.Component; 
import java.awt.event.KeyEvent; 

import javax.swing.JFrame; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.MenuElement; 
import javax.swing.MenuSelectionManager; 
import javax.swing.event.ChangeEvent; 
import javax.swing.event.ChangeListener; 

public class Menu { 

    public static void main(final String args[]) { 
    JFrame frame = new JFrame("MenuSample Example"); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
JMenuBar menuBar = new JMenuBar(); 

JMenu worldMenu = new JMenu("world"); 
menuBar.add(worldMenu); 


JMenu indMenu = new JMenu("India"); 
worldMenu.add(indMenu); 
/* creates menu */ 
JMenu odMenu = new JMenu("Odisha"); 
indMenu.add(odMenu); 

JMenu delhiMenu = new JMenu("Delhi"); 
indMenu.add(delhiMenu); 

JMenu upMenu = new JMenu("Uttar Pradesh"); 
indMenu.add(upMenu); 

JMenu mpMenu = new JMenu("Madhya Pradesh"); 
    indMenu.add(mpMenu); 

    JMenu ausMenu = new JMenu("Australia"); 
worldMenu.add(ausMenu); 

JMenu AmericaMenu = new JMenu("America"); 
worldMenu.add(AmericaMenu); 

/* creates submenu */ 
JMenu bbMenu = new JMenu("Bhubaneswar"); 
odMenu.add(bbMenu); 

JMenu bmMenu = new JMenu("Berhampur"); 
odMenu.add(bmMenu); 
/*creates sub sub menu */ 
JMenuItem rjMenuItem = new JMenuItem("Raj Mahal"); 
bbMenu.add(rjMenuItem); 

JMenuItem abMenuItem = new JMenuItem("Acharya Bihar"); 
bbMenu.add(abMenuItem); 

JMenuItem bnMenuItem = new JMenuItem("Bani Bihar"); 
bbMenu.add(bnMenuItem); 
/* retrieving path */ 

MenuSelectionManager.defaultManager().addChangeListener(
     new ChangeListener() { 
      public void stateChanged(ChangeEvent evt) { 

      MenuElement[] path = MenuSelectionManager.defaultManager() 
       .getSelectedPath(); 

      // 
      int s=0; 

      for (int i = 0; i < path.length; i++) { 
       Component c = path[i].getComponent(); 
       if (c instanceof JMenuItem) { 
       JMenuItem mi = (JMenuItem) c; 
       String label = mi.getText(); 
       System.out.println("LEVEL----" + s); 
       System.out.println("you hv selected:"+label); 
       s++; 


       } 
      } 

      } 
     }); 

    // 

    frame.setJMenuBar(menuBar); 
    frame.setSize(350, 250); 
    frame.setVisible(true); 
    } 
} 
+2

請發表你試過嗎? –

+0

發佈了代碼@TamilSelvan –

+0

什麼不是在你的代碼中工作? – MadProgrammer

回答

0

如何獲得當前選定的

  1. 菜單 - JMenu的,不能選擇父母。爲什麼你想知道 如果鼠標結束了?
  2. 菜單項 - 擁抱Action接口

這是一個很常見的監督,不使用Action interface。當用Swing開發使Action成爲你的朋友時,它會很好的對待你。你用MenuSelectionManager去了錯誤的路徑。

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 

public class MenuExample { 
    private JFrame frame; 
    private JLabel choiceIndicator; 

    MenuExample create() { 
     frame = createFrame(); 
     choiceIndicator = new JLabel(); 
     frame.setJMenuBar(createMenuBar()); 
     frame.getContentPane().add(createContent()); 

     return this; 
    } 

    private Component createContent() { 
     JPanel panel = new JPanel(); 

     panel.add(new JLabel("Last menu item choice:")); 
     panel.add(choiceIndicator); 

     return panel; 
    } 

    private JMenuBar createMenuBar() { 
     JMenuBar menuBar = new JMenuBar(); 
     menuBar.add(createWorld()); 
     return menuBar; 
    } 

    private JMenu createWorld() { 
     JMenu worldMenu = new JMenu("World"); 

     worldMenu.add(createIndia()); 
     worldMenu.add(new JMenu("Australia")); 
     worldMenu.add(new JMenu("America")); 

     return worldMenu; 
    } 

    private JMenu createIndia() { 
     JMenu india = new JMenu("India"); 

     india.add(createOdisha()); 
     india.add(new JMenu("Delhi")); 
     india.add(new JMenu("Uttar Pradesh")); 
     india.add(new JMenu("Madhya Pradesh")); 

     return india; 
    } 

    private JMenuItem createOdisha() { 
     JMenu menu = new JMenu("Odisha"); 

     menu.add(createBhubaneswar()); 
     menu.add(new JMenu("Berhampur")); 

     return menu; 
    } 

    private JMenuItem createBhubaneswar() { 
     JMenu menu = new JMenu("Bhubaneswar"); 
     menu.add(choiceItem("Raj Mahal")); 
     menu.add(choiceItem("Acharya Bihar")); 
     menu.add(choiceItem("Bani Bihar")); 

     return menu; 
    } 

    private JMenuItem choiceItem(String text) { 
     return new JMenuItem(new Choice(text, choiceIndicator)); 
    } 

    private JFrame createFrame() { 
     JFrame frame = new JFrame(getClass().getSimpleName()); 
     frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 

     return frame; 
    } 

    void show() { 
     frame.setSize(350, 250); 
     frame.setResizable(false); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new MenuExample().create().show(); 
      } 
     }); 
    } 

    class Choice extends AbstractAction { 
     private final JLabel choiceIndicator; 

     public Choice(String text, JLabel choiceIndicator) { 
      this(text, null, null, null, choiceIndicator); 
     } 

     public Choice(String text, ImageIcon icon, String desc, Integer mnemonic, JLabel choiceIndicator) { 
      super(text, icon); 
      putValue(SHORT_DESCRIPTION, desc); 
      putValue(MNEMONIC_KEY, mnemonic); 

      this.choiceIndicator = choiceIndicator; 
     } 

     public void actionPerformed(ActionEvent e) { 
      choiceIndicator.setText(e.getActionCommand()); 
     } 
    } 
} 
+0

非常感謝主席先生,我會牢記所有這些你提到的概念。 –

相關問題