The following illustrates use of ActionListener with AWT MenuItem.
MenuItemAction(): Code for using
Note: To fire the
import java.awt.*;
import java.awt.event.*;
class MenuItemAction extends Frame implements ActionListener
{
MenuBar mb;
Menu m;
MenuItem exit;
public MenuItemAction()
{
createAndShowGUI();
}
private void createAndShowGUI()
{
setTitle("ActionListener for MenuItem");
// Create MenuBar, Menu, MenuItem
mb=new MenuBar();
m=new Menu("Menu");
exit=new MenuItem("Exit");
// Add ActionListener
exit.addActionListener(this);
// Add exit to sajian and sajian to menubar
m.add(exit);
mb.add(m);
// Set sajian kafe to frame
setMenuBar(mb);
setSize(400,400);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
System.exit(0);
}
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 sajian shortcut.