开发者

a GUI java program needing an action event button

开发者 https://www.devze.com 2023-03-28 11:51 出处:网络
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 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);
        }

}


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


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


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.


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消