使用 ActionListener 的退出菜单不起作用

Posted

技术标签:

【中文标题】使用 ActionListener 的退出菜单不起作用【英文标题】:Exit Menu using ActionListener is not working 【发布时间】:2021-08-16 10:33:21 【问题描述】:

我正在尝试使用 ActionListener 退出菜单,但是我的代码无法正常工作,并且“退出程序”按钮没有任何作用。下面是代码:

import java.awt.*;
 import java.awt.event.*;
 import javax.swing.JMenu;
 import javax.swing.JMenuItem;
 import javax.swing.JCheckBoxMenuItem;
 import javax.swing.JRadioButtonMenuItem;
 import javax.swing.ButtonGroup;
 import javax.swing.JMenuBar;
 import javax.swing.KeyStroke;
 import javax.swing.ImageIcon;
 import javax.swing.JPanel;
 import javax.swing.JTextArea;
 import javax.swing.JScrollPane;
 import javax.swing.JFrame;

 public class testCoinSorterGUI extends testCoinSorter

JTextArea output;
JScrollPane scrollPane;

public JMenuBar createMenuBar() 
    JMenuBar menuBar;
    JMenu menu, submenu;
    JMenuItem menuItem;
    JRadioButtonMenuItem rbMenuItem;
    JCheckBoxMenuItem cbMenuItem;

    //Create the menu bar.
    menuBar = new JMenuBar();

    //Build the first menu.
    menu = new JMenu("Calculator");
    menu.setMnemonic(KeyEvent.VK_A);
    menu.getAccessibleContext().setAccessibleDescription(
            "The only menu in this program that has menu items");
    menuBar.add(menu);
    
    menuItem = new JMenuItem("Coin calculator");
    menu.add(menuItem);
    
    menuItem = new JMenuItem("Multi Coin calculator");
    menu.add(menuItem);

    //Build second menu in the menu bar.
    menu = new JMenu("Print coin list");
    menu.setMnemonic(KeyEvent.VK_N);
    menu.getAccessibleContext().setAccessibleDescription("This menu does nothing");
    menuBar.add(menu);

    //Build third menu in the menu bar.
    menu = new JMenu("Set details");
    menuBar.add(menu);

    menuItem = new JMenuItem("Set currency");
    menu.add(menuItem);

    menuItem = new JMenuItem("Set minimum coin input value");
    menu.add(menuItem);

    menuItem = new JMenuItem("Set maximum coin input value");
    menu.add(menuItem);
    menu.addSeparator();

    menuItem = new JMenuItem("Display program configurations");
    menu.add(menuItem);

    //Build fifth menu in the menu bar.
    menu = new JMenu("Quit the program");
    menuBar.add(menu);
    menu.addActionListener(
        new ActionListener() 
            public void actionPerformed(ActionEvent e) 
                System.exit(0);
            
        
    );
    
    return menuBar;


public Container createContentPane() 
    //Create the content-pane-to-be.
    JPanel contentPane = new JPanel(new BorderLayout());
    contentPane.setOpaque(true);

    //Create a scrolled text area.
    output = new JTextArea(5, 30);
    output.setEditable(false);
    scrollPane = new JScrollPane(output);

    //Add the text area to the content pane.
    contentPane.add(scrollPane, BorderLayout.CENTER);

    return contentPane;


/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) 
    java.net.URL imgURL = testCoinSorterGUI.class.getResource(path);
    if (imgURL != null) 
        return new ImageIcon(imgURL);
     else 
        System.err.println("Couldn't find file: " + path);
        return null;
    


/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.
 */
private static void createAndShowGUI() 
    //Create and set up the window.
    JFrame frame = new JFrame("*** CoinSorterGUI ***");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    testCoinSorterGUI demo = new testCoinSorterGUI();
    frame.setJMenuBar(demo.createMenuBar());
    frame.setContentPane(demo.createContentPane());

    //Display the window.
    frame.setSize(680, 480);
    frame.setVisible(true);


public static void main(String[] args) 
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() 
        public void run() 
            createAndShowGUI();
        
    );


目前我只想测试“退出程序”按钮,所以请忽略所有其他菜单。任何帮助将不胜感激,尽管我认为我已将代码按正确的顺序排列,但我真的很困惑。

【问题讨论】:

JMenu 不被视为“可操作”项目。您需要将JMenuItem 添加到JMenu,然后才能执行操作。这是设计使然。或者,如果你真的不喜欢你的用户,你可以做一些事情like this 这种性质的问题不需要 >130 LOC 来证明问题。为了尽快获得更好的帮助,edit 添加minimal reproducible example 或Short, Self Contained, Correct Example。 【参考方案1】:

您可以使用它来代替 ActionListener,它应该用于不同范围的控件:

JMenu quitProgramMenu = new JMenu("Quit the program");

quitProgramMenu.addMenuListener(
 new MenuListener() 

  @Override
  public void menuSelected(MenuEvent e) 
   System.exit(0);
  

  @Override
  public void menuDeselected(MenuEvent e) 

  @Override
  public void menuCanceled(MenuEvent e) 
 
);
menuBar.add(quitProgramMenu);

【讨论】:

以上是关于使用 ActionListener 的退出菜单不起作用的主要内容,如果未能解决你的问题,请参考以下文章

ActionListener添加到JButton但不起作用

将 JMenuItem 的名称赋予它的 ActionListener

JSF action、actionlistener、onClick之间的区别[重复]

JMenu ActionListener

在实现actionListener的jpanels之间切换[重复]

ActionListener的三种实现方法