Java 菜单项赢得';我对点击没有反应

Java 菜单项赢得';我对点击没有反应,java,swing,jmenuitem,Java,Swing,Jmenuitem,“退出”菜单项响应完美,但“更改名称”是一个问题。。我不知道怎么了,有人能帮我吗。。。我知道userName.setUserName工作正常。。因为我在另一个课堂上使用它,它工作得非常好。就是不知道怎么了 这是密码 public class MenuBar extends JMenuBar{ // variable declaration private JMenu menu; private JMenuItem menuItem; private JMenuI

“退出”菜单项响应完美,但“更改名称”是一个问题。。我不知道怎么了,有人能帮我吗。。。我知道userName.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);
            }

    }

}
}

它可能正在工作,但是您没有得到反馈,因为您正在将名称更改为上一个名称。如果在setUserName方法中添加了一些其他值,您将看到

您从未使用
更改的名称注册
菜单句柄

此外,请注意,您正在多次重新分配
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);
        }

    }
}
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);
        }

    }
}
一般来说,您确实需要扩展
JMenuBar
,但只需创建一个实例并向其中添加所需的菜单即可

你也可以考虑看一下,它们是自包含的单元,它们包含它们自己的配置和动作逻辑

例如

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);
        }

    }
}
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);
        }

    }
}

问题似乎是
menuItem\u Command.equals(“更改名称”)
要检查自己到底发生了什么,我建议您告诉我您是否使用了不同的想法实际上是。。真不敢相信我错过了。。我已经登记了出口,但忘记了通道的名字。。多谢你pro@Juice很容易错过