2015-04-28 119 views
0

「退出」MenuItem完美響應,但「更改名稱」是一個問題..我無法弄清楚什麼是錯的,有人可以幫助請...我知道用戶名。 setUserName工作正常..因爲我在另一個類中使用它,它完美的作品。只是無法弄清楚什麼是錯菜單項不會響應點擊

下面的代碼

public class MenuBar extends JMenuBar{ 

    // variable declaration 
    private JMenu menu; 
    private JMenuItem menuItem; 
    private JMenuItem changed_Name; 
    private JMenuItem exit; 



    public MenuBar(){ 

     init(); 

    } 


    private void init(){ 

     menu = new JMenu("File"); 
     add(menu); 

     changed_Name = new JMenuItem("Change Name"); 
     changed_Name.setMnemonic(KeyEvent.VK_C); 
     changed_Name.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, KeyEvent.ALT_MASK)); 
     changed_Name.getAccessibleContext().setAccessibleDescription("This Will Allow a Name Change"); 
     menu.add(changed_Name); 

    exit = new JMenuItem("Exit"); 
    menu.add(exit); 


    menu = new JMenu("Help"); 
    add(menu); 

    menuItem = new JMenuItem("Help & Docs"); 
    //menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, KeyEvent.CHAR_UNDEFINED)); 
    menuItem.getAccessibleContext().setAccessibleDescription("Get Help or View Software documents"); 
    menu.add(menuItem); 

    menuItem = new JMenuItem("About"); 
    menu.add(menuItem); 

    MenuHandler menuHandler = new MenuHandler(); 
    menuItem.addActionListener(menuHandler); 
    exit.addActionListener(menuHandler); 


} 


private class MenuHandler implements ActionListener{ 

    AgentName userName = new AgentName(); 

    @Override 
    public void actionPerformed(ActionEvent e) throws UnsupportedOperationException { 

      Object menuItem_Command = e.getActionCommand(); 

      if(menuItem_Command.equals("Change Name")){ 
       userName.setUserName(userName.getUserName()); 
      } 
      else if(menuItem_Command.equals("Exit")){ 
       System.exit(0); 
      } 

    } 

} 
} 
+0

問題似乎是'menuItem_Command.equals(「更改名稱」)'來檢查自己是怎麼回事,我建議你[查看如何調試Eclipse中的應用(HTTP:/ /www.vogella.com/tutorials/EclipseDebugging/article.html)告訴我你是否使用不同的IDE –

回答

2

你從來沒有註冊MenuHandlerchanged_nameJMenuItem ...

此外,要小心,你重新分配menuItem多次,這意味着只有帽子是About了與MenuHandler

例如註冊...

import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import javax.swing.JFrame; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JPanel; 
import javax.swing.KeyStroke; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Test { 

    public static void main(String[] args) { 
     new Test(); 
    } 

    public Test() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setJMenuBar(new MenuBar()); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class MenuBar extends JMenuBar { 

     // variable declaration 
     private JMenu menu; 
     private JMenuItem menuItem; 
     private JMenuItem changed_Name; 
     private JMenuItem exit; 

     public MenuBar() { 

      init(); 

     } 

     private void init() { 

      menu = new JMenu("File"); 
      add(menu); 

      MenuHandler menuHandler = new MenuHandler(); 

      changed_Name = new JMenuItem("Change Name"); 
      changed_Name.setMnemonic(KeyEvent.VK_C); 
      changed_Name.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, KeyEvent.ALT_MASK)); 
      changed_Name.getAccessibleContext().setAccessibleDescription("This Will Allow a Name Change"); 
      changed_Name.addActionListener(menuHandler); 
      menu.add(changed_Name); 

      exit = new JMenuItem("Exit"); 
      menu.add(exit); 

      menu = new JMenu("Help"); 
      add(menu); 

      menuItem = new JMenuItem("Help & Docs"); 
      //menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, KeyEvent.CHAR_UNDEFINED)); 
      menuItem.getAccessibleContext().setAccessibleDescription("Get Help or View Software documents"); 
      menuItem.addActionListener(menuHandler); 
      menu.add(menuItem); 

      menuItem = new JMenuItem("About"); 
      menuItem.addActionListener(menuHandler); 
      menu.add(menuItem); 

      exit.addActionListener(menuHandler); 

     } 

     private class MenuHandler implements ActionListener { 

//   AgentName userName = new AgentName(); 

      @Override 
      public void actionPerformed(ActionEvent e) throws UnsupportedOperationException { 

       Object menuItem_Command = e.getActionCommand(); 

       System.out.println(menuItem_Command); 

       if (menuItem_Command.equals("Change Name")) { 
        System.out.println("Yippe"); 
//     userName.setUserName(userName.getUserName()); 
       } else if (menuItem_Command.equals("Exit")) { 
        System.exit(0); 
       } 

      } 

     } 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(200, 200); 
     } 

    } 
} 

一般來說,你應該真正需要EXT結束JMenuBar,而是簡單地創建一個實例,並添加你需要把它

您也可以考慮在看看How to Use Actions菜單,這是自包含單位,其中包含自己的配置和行動邏輯

例如...

import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import javax.swing.AbstractAction; 
import static javax.swing.Action.ACCELERATOR_KEY; 
import static javax.swing.Action.MNEMONIC_KEY; 
import static javax.swing.Action.NAME; 
import javax.swing.JFrame; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JPanel; 
import javax.swing.KeyStroke; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Test { 

    public static void main(String[] args) { 
     new Test(); 
    } 

    public Test() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setJMenuBar(new MenuBar()); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class ChangeNameAction extends AbstractAction { 

     public ChangeNameAction() { 
      putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_1, KeyEvent.ALT_MASK)); 
      putValue(MNEMONIC_KEY, KeyEvent.VK_C); 
      putValue(NAME, "Change Name"); 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      System.out.println("Change name"); 
     } 
    } 

    public class ExitAction extends AbstractAction { 

     public ExitAction() { 
      putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_4, KeyEvent.ALT_MASK)); 
      putValue(MNEMONIC_KEY, KeyEvent.VK_X); 
      putValue(NAME, "Exit"); 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      System.exit(0); 
     } 
    } 

    public class HelpAction extends AbstractAction { 

     public HelpAction() { 
      putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0)); 
      putValue(MNEMONIC_KEY, KeyEvent.VK_H); 
      putValue(NAME, "Help & Docs"); 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      System.out.println("Help and docs"); 
     } 
    } 

    public class AboutAction extends AbstractAction { 

     public AboutAction() { 
      putValue(NAME, "About"); 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      System.out.println("About"); 
     } 
    } 

    public class MenuBar extends JMenuBar { 

     // variable declaration 
     private JMenu menu; 
     private JMenuItem menuItem; 
     private JMenuItem changed_Name; 
     private JMenuItem exit; 

     public MenuBar() { 

      init(); 

     } 

     private void init() { 

      menu = new JMenu("File"); 
      add(menu); 

      menu.add(new ChangeNameAction()); 
      menu.add(new ExitAction()); 

      menu = new JMenu("Help"); 
      add(menu); 

      menu.add(new HelpAction()); 
      menu.add(new AboutAction()); 

     } 

     private class MenuHandler implements ActionListener { 

    //   AgentName userName = new AgentName(); 
      @Override 
      public void actionPerformed(ActionEvent e) throws UnsupportedOperationException { 

       Object menuItem_Command = e.getActionCommand(); 

       System.out.println(menuItem_Command); 

       if (menuItem_Command.equals("Change Name")) { 
        System.out.println("Yippe"); 
        //     userName.setUserName(userName.getUserName()); 
       } else if (menuItem_Command.equals("Exit")) { 
        System.exit(0); 
       } 

      } 

     } 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(200, 200); 
     } 

    } 
} 
+0

它實際上是..不能相信我錯過了..我已經註冊退出但忘記了Chane Name ..感謝很多瘋狂的親 – Juice

+0

@Juice很容易錯過 – MadProgrammer

0

它可能是工作,但由於要更改名稱前一個你沒有得到反饋。如果你把一些其他的價值在setUserName方法,你會看到

+0

謝謝曼.. bu我只是忘了添加聽衆 – Juice