samedi 16 juillet 2016

Using an action listener for java to exit a program when a Jmenu button is pressed

I'm attempting to exit a program when the user hits the exit JMenu item.

This is the class I'm using to execute the action listener:

public abstract class ExitListener implements ActionListener {
    public void exit(ActionEvent e) {
        if (e.getActionCommand().equals("exit")) {
            int reply = JOptionPane.showConfirmDialog(null, "Are you sure?", "Quit?", JOptionPane.YES_NO_OPTION);
            if (reply == JOptionPane.YES_OPTION) {
                System.exit(0);
            }
        }
    }
}

This is how I initialised my button:

menuBar = new JMenuBar();
gameMenu = new JMenu("Game");
this.setJMenuBar(menuBar);
menuBar.add(gameMenu);

// Creates the tabs for the Game menu and add's it to the game menu
exit = new JMenuItem("Exit");
gameMenu.add(exit);

When I select the exit button on the menu, nothing happens.

Aucun commentaire:

Enregistrer un commentaire