- AWT Menu Test Program

This section provides a tutorial example on how to use the java.awt.Menu class to create multiple menus and how to add them to the menu bar. Sub menus and menu items added to a menu will be listed vertically when the menu is selected.

Here is an example program I wrote to test the java.awt.Menu class:

 * Copyright (c) All Rights Reserved.
import java.awt.Frame; 
import java.awt.MenuBar; 
import java.awt.Menu; 
import java.awt.MenuItem; 
public class MenuTest {
   Frame myFrame = null;
   public static void main(String[] a) {
      (new MenuTest()).test();
   private void test() {
      myFrame = new Frame("Menu Test");
      MenuBar myMenuBar = new MenuBar();
      Menu myMenu = getFileMenu();
      myMenu = getColorMenu();

   private Menu getFileMenu() {
      Menu myMenu = new Menu("File");
      Menu mySubMenu = getOpenMenu();
      MenuItem myItem = new MenuItem("Close");
      myItem = new MenuItem("Exit");
      return myMenu;
   private Menu getColorMenu() {
      Menu myMenu = new Menu("Color");
      MenuItem myItem = new MenuItem("Red");
      myItem = new MenuItem("Green");
      myItem = new MenuItem("Blue");
      return myMenu;
   private Menu getOpenMenu() {
      Menu myMenu = new Menu("Open");
      MenuItem myItem = new MenuItem("Java");
      myItem = new MenuItem("HTML");
      myItem = new MenuItem("GIF");
      return myMenu;

If you run this example, you will see the frame window shows up with the menu bar like this:

AWT Menu Test
AWT Menu Test

Interesting notes about this tutorial example:

Table of Contents

 About This Book

 JDK (Java Development Kit)

 Introduction of Java Swing Package

 Graphics Environment of the Local System

 JFrame - Main Frame Class

 JLabel - Swing Label Class

 JButton - Swing Button Class

 JCheckBox - Swing Check Box Class

 JRadioButton - Swing Radio Button Class

 JTextField - Swing Text Field Class

 JComboBox - Swing Combo Box Class

 Menu Bar, Menus, Menu Items and Listeners

 Creating Internal Frames inside the Main Frame

 Layout of Components in a Container

 LookAndFeel and UIManager

 Option Dialog Boxes

 JEditorPane - The Editor Pane Class

 SwingWorker - The Background Task Worker

AWT (Abstract Windows Toolkit)

 What Is AWT (Abstract Windows Toolkit) - My First AWT Program

 Closing AWT Frame and Terminating Application

 Drawing Graphics - Using paint() on Frame or Component

 Creating Labels with java.awt.Label Class

 Creating Buttons with java.awt.Button Class

 AWT Button Action Handler at the Component Level

 AWT Button Action Handler at the Frame Level

 AWT Button Mouse Click Handler at the Frame Level

 AWT TextField and ActionListener - AWT Menu Bar Test Program - AWT Menu Test Program - AWT Menu Item Test Program - AWT Menu Item Action Listener

 Integration with Desktop System

 Outdated Tutorials


 Full Version in PDF/EPUB