Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Basic Menu problem

  1. #1
    Junior Member
    Join Date
    Oct 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Basic Menu problem

    I'm having a problem getting the Exit option in my Menu to execute the System.exit(0); line in my code.
    Any help would be greatly appreciated.
    I'm pretty new to forums, and I can't figure out how to attach my code, so I'll just paste it here. The "Manage Attachments" button didn't do anything.
    Also, I had to replace the AT symbol (shift 2) with "at" in order to post my question.

     
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
     
    public class MenuTest {
        private static void createAndShow() {
     
            JFrame frame = new JFrame("FrameDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            JLabel emptyLabel = new JLabel("");
            emptyLabel.setPreferredSize(new Dimension(500, 500));
            frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
     
            JMenuBar menuBar = new JMenuBar();
            JMenu menuExit = new JMenu("Exit");
     
            menuBar.add(menuExit);
            frame.setJMenuBar(menuBar);
     
            menuExit.addActionListener(new ActionListener() {
                "at"Override
                public void actionPerformed(ActionEvent e) {
                    String cmd = e.getActionCommand();
                    if (cmd == "Exit")
                        System.exit(0);
                }
            });
     
            frame.pack();
            frame.setVisible(true);
        }
     
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShow();
                }
            });
     
        }
    }

  2. #2
    Junior Member
    Join Date
    Oct 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Basic Menu problem

    I solved it by using MenuListener instead of ActionListener.

Similar Threads

  1. Popup menu problem
    By Ruthvik in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 19th, 2012, 07:37 AM
  2. Menu Problem
    By StuckInJava in forum Loops & Control Statements
    Replies: 4
    Last Post: March 23rd, 2012, 06:43 PM
  3. Switch problem. How do I return to my menu?
    By jonny007 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 28th, 2012, 12:54 PM
  4. problem with help menu.
    By Toggo in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 12th, 2010, 05:13 PM
  5. Problem while implementing a basic user interface menu
    By Rastabot in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 3rd, 2009, 04:38 PM