3

I am making a simple java program that represents a Microsoft Word menu bar, and in the file menu I add an exit button... it does nothing.

I need your help to tell me what to do to make the exit button actually exit and close the program.

Here is my code:

public class MicrosoftWordMenu {
        public static void main(String [] args)
        {
        JPanel panel = new JPanel();
        JFrame frame = new JFrame("Microsoft Word");
        JMenuBar bar = new JMenuBar();
        JMenu menu = new JMenu("File");
        JMenu menu1 = new JMenu("Edit");
        JMenu menu2 = new JMenu("View");
        JMenu menu3 = new JMenu("Insert");
        JMenu menu4 = new JMenu("Format");
        JMenu menu5 = new JMenu("Tools");
        JMenu menu6 = new JMenu("Table");
        JMenu menu7 = new JMenu("Window");
        JMenu menu8 = new JMenu("Help");

        frame.add(bar, BorderLayout.NORTH);
        frame.add(panel);

        bar.add(menu);
        bar.add(menu1);
        bar.add(menu2);
        bar.add(menu3);
        bar.add(menu4);
        bar.add(menu5);
        bar.add(menu6);
        bar.add(menu7);
        bar.add(menu8);

        JMenuItem menuitem = new JMenuItem("New...");
        JMenuItem menuitem1 = new JMenuItem("Open...");
        JMenuItem menuitem2 = new JMenuItem("Close");
        JMenuItem menuitem3 = new JMenuItem("Save");
        JMenuItem menuitem4 = new JMenuItem("Save as...");
        JMenuItem menuitem5 = new JMenuItem("Save as web page...");
        JMenuItem menuitem6 = new JMenuItem("Web page preview ");
        JMenuItem menuitem7 = new JMenuItem("Print ");
        JMenuItem menuitem8 = new JMenuItem("Exit");

        menu.add(menuitem);
        menu.add(menuitem1);
        menu.add(menuitem2);
        menu.add(menuitem3);
        menu.add(menuitem4);
        menu.add(menuitem5);
        menu.add(menuitem6);
        menu.add(menuitem7);
        menu.add(menuitem8);

        frame.setSize(600,100);
        frame.setVisible(true);
        }

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user898342
  • 31
  • 2

5 Answers5

4

Also consider using Action to let components share functionality, as shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
3

Calling System.exit(0) on menu selection would do just fine.

Vlad
  • 10,602
  • 2
  • 36
  • 38
2

At the moment you're just creating the GUI element, but they're basically empty shells. In particular, you've created items that appear on the menu, but you haven't added any behaviour to those items. (Since you haven't told your program anywhere what to do when an item is clicked, what did you think would happen)?

In order to provide this behaviour, you'll need to register an ActionListener, which will be called when something happens on an element, and will let you take an appropriate action at that point (such as calling System.exit(0). See the tutorial on Writing [Swing] Event listeners for details.

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
  • I know I made them empty for now, I will add a few latter, thanks for the action listener thing, I don't know how I forgot about it ..... still can you show me where to put it? ( I mean the System.exit(0) thing) and how do I make it so it works with the exit button – user898342 Aug 17 '11 at 10:12
  • @kleopatra - +1 on the assumption that ActionListener isn't the best way to achieve this. I suggest you write an alternative answer using `Action` though, as I've only ever worked with the listeners and a fuller explanation/illustration would be much more useful to myself and others. – Andrzej Doyle Aug 17 '11 at 10:50
2

I always extend the AbstractAction class (which implements the ActionListener interface) which allows you to reuse your actions. By extending the AbstractAction, the actionPerformed(ActionEvent e) method will be invoked very time your button is clicked.

public class CloseAction extends AbstractAction {

  public CloseAction() {
    super("Close");
  }

  public actionPerformed(ActionEvent e) {
    System.exit(1);  
  }

}

Now, to apply the above action to one of your buttons, you simply need to follow the below piece of code.

CloseButton.setAction(new CloseAction());

The close button will now shut down your application each time it gets pressed.

sbrattla
  • 5,274
  • 3
  • 39
  • 63
2

Use the ExitAction defined in Closing an Application. Then clicking on the Exit menu item will be just like clicking on the Close icon at the top right of the window. This allows for consistency when closing an application in case you ever need to do close processing.

Edit:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
...
// JMenuItem menuitem8 = new JMenuItem("Exit");
JMenuItem menuitem8 = new JMenuItem( new ExitAction() );
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thanks man that helped me and I learned new stuff, but I still couldn't do it to my program !!!! why is it so hard? it looks easy but I cant get the hang of it... can someone do it to my program ? I think I will understand it more if I saw how it was done – user898342 Aug 18 '11 at 02:52