Using ActionListener for AWT MenuItem

The following illustrates use of ActionListener with AWT MenuItem.

import java.awt.*;
import java.awt.event.*;
class MenuItemAction extends Frame implements ActionListener
MenuBar mb;
Menu m;
MenuItem exit;

    public MenuItemAction()
    private void createAndShowGUI()
        setTitle("ActionListener for MenuItem");
        // Create MenuBar, Menu, MenuItem
        mb=new MenuBar();
        m=new Menu("Menu");
        exit=new MenuItem("Exit");
        // Add ActionListener
        // Add exit to menu and menu to menubar
        // Set menu bar to frame
    public void actionPerformed(ActionEvent ae)
    public static void main(String args[])
        new MenuItemAction();

MenuItemAction(): Code for using ActionListener with MenuItem is written here.
Note: To fire the ActionEvent you need to click on the MenuItem or select it and hit enter. Alternatively, you can also set a menu shortcut.

Using ActionListener for AWT MenuItem

Posted by at on

Tags: AWT Event Handling,