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