Hey, I am trying to implement a ctrl + x to close my program when the user clicks on the exit menu item or hits ctrl + x, but so far it is unresponsive, here is my code:

package main;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

import testing.Intro;

public class MainProgram extends JFrame implements ActionListener
{
/*
 * Variables for the GUI. These must be private and static since they will be accessed anywhere and have no
 * need to be changed.
 */	
private static JFrame frame;	
private static JMenuBar menuBar;
private static JMenu fileMenu, helpMenu;
private static JMenuItem newItem, openItem, exitItem, aboutItem;
	
	private static void createAndShowGUI()
	{
		// Create the frame and configure settings
		frame = new JFrame("Soccer Manager");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setSize(1000, 800);
        
		// Add the label to the program
        JLabel label = new JLabel("Welcome!");
        frame.getContentPane().add(label);  
        
        // Add the menu to the program
        menuBar = new JMenuBar();
        // Make the file menu
        fileMenu = new JMenu("File");
        menuBar.add(fileMenu);
        fileMenu.setMnemonic(KeyEvent.VK_F);
        // New menu item
        newItem = new JMenuItem("New");
        fileMenu.add(newItem);
        // Open menu item
        openItem = new JMenuItem("Open");
        fileMenu.add(openItem);        
        // Exit menu item
        exitItem = new JMenuItem("Exit");
		exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
		ActionEvent.CTRL_MASK));        
		fileMenu.add(exitItem);
        // Make the help menu
        helpMenu = new JMenu("Help");
        menuBar.add(helpMenu);
        helpMenu.setMnemonic(KeyEvent.VK_H);
        // About menu item
        JMenuItem aboutItem = new JMenuItem("About");
        helpMenu.add(aboutItem);        
        // Adds the menuBar to the frame
        frame.setJMenuBar(menuBar); 
	}
	
	public void actionPerformed(ActionEvent e)
	{   

                if(e.getSource() == exitItem)
		{
			System.exit(0);
		}	
	}
	
	public static void main(String[] args)
	{
        javax.swing.SwingUtilities.invokeLater(new Runnable() 
        {
            public void run() 
            {
                createAndShowGUI();
                //testing.Intro.welcomeMessage();
            }
        });		
	}
}

Rather than implementing the ActionListener in your class, it is probably better to add an ActionListener to your menu item. You can do this by

// Exit menu item
exitItem = new JMenuItem("Exit");
exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,   ActionEvent.CTRL_MASK));
exitItem.addActionListener(new ActionListener() {
   public void actionEvent(Event e) {
      System.exit(0);
   }
});
fileMenu.add(exitItem);

However, if you need action listeners for every item in your GUI this code will create many classes, which may or may not be desired.

Another way of doing this would be to create your own actionListener class as an inner class and put the implentation in there, similar to your code which reads

public void actionPerformed(ActionEvent e)
{
if(e.getSource() == exitItem)
{
System.exit(0);
}
}

but in another class. Then add that ActionListener to each of your items.

Hope this helps,
darkagn

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.