Java Exit Button from a MenuItem/MenuBar and Sound Player

48,070

Solution 1

To make the exit menu work, use a JMenuItem:

  JMenuItem exit = new JMenuItem("Exit");
  exit.addActionListener(new exitApp());
  file.add(exit);

Regarding your other question, how to make the button "smaller", you need to understand that you're adding this JButton to a JFrame's contentPane, and that contentPane's use BorderLayout by default. So adding the button in this way will make it fill up the container completely. To prevent this from happening, you'll need to use other layouts. Please read up on how to use Swing Layouts for the details: A Visual Guide to the Layout Managers

Solution 2

Below is how i implement it in my applications, hope it helps:

    menuBar = new JMenuBar();
    mainWindow.setJMenuBar(menuBar);

    mnFile = new JMenu("File");
    menuBar.add(mnFile);

    mntmClose = new JMenuItem("Close");
    mntmClose.setMnemonic(KeyEvent.VK_Q);
    mntmClose.setAccelerator(KeyStroke.getKeyStroke(
             KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
    mntmClose.setToolTipText("Exit application");
    mntmClose.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.exit(0);
        }

    });
    mnFile.add(mntmClose);
Share:
48,070
Master345
Author by

Master345

Updated on July 09, 2022

Comments

  • Master345
    Master345 almost 2 years

    Ok, i wrote this pice of code as an beginner in java learning from videos/school, and i got some questions.

    1 => Why the File > Exit button is not working and have a small arrow as if having some childs? The big exit button works with the same function. I have been inspired from here: http://www.youtube.com/watch?src_vid=FB_wJpIdA8k&feature=iv&annotation_id=annotation_40248&v=dwLkDGm5EBc

    2 => How can i make that button smaller? It's bigger when i resize it.

    3 => Does anyone know a simple sound player library? So when i press that button to play a sound? I have tryied some web examples, like http://www.developer.com/java/other/article.php/2173111/Java-Sound-Playing-Back-Audio-Files-using-Java.htm and don't know how to make it simple and use it everywhere like SoundPlay(sound.au);

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    
    public class form4 
    {
        public static void main(String[] args)
        {
            // Frame
            JFrame frame = new JFrame("Menu");
            frame.setSize(300,300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            // Just create menubar
            JMenuBar menubar = new JMenuBar();
            frame.setJMenuBar(menubar);
    
            // Add an JMenu
            JMenu file = new JMenu("File");
            menubar.add(file);
            // Add an JMenuItem
            JMenuItem exit = new JMenu("Exit");
            file.add(exit);
            exit.addActionListener(new exitApp());
    
            // Add an JMenu
            JMenu help = new JMenu("Help");
            menubar.add(help);
            // Add an JMenuItem
            JMenuItem about = new JMenuItem("About");
            help.add(about);
    
            // Add an JButton
            JButton exitButton= new JButton("Exit!");
            frame.add(exitButton);
            exitButton.addActionListener(new exitApp());
            exitButton.setSize(40,40);
    
            frame.setVisible(true);
        }
    
        // Exit app
        static class exitApp implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        }
    }
    

    Thank you!