AWT Menu Based Application in Java with Examples

AWT Menu Based Application in Java with Examples


Table of Contents

a) Introduction to Menu Based Application
b) Example with Source Code
c) Output (Image and Video )

************
Creating AWT (Abstract Window Toolkit) Menu based application is similar to the SWING Menu Based Application. We have to use three classes for creating the AWT Menu Based Application. MenuBar, Menu and MenuItem are the classes required. These classes are in the java.awt package.

If you don’t know to create the AWT Frame then visit here and understand to create the frame.

Let us see the source code and its output and understand;

Source Code:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package AWTComponents;

/**
 *
 * @author AnkitPC
 */
import java.awt.event.*;
import java.awt.*;
public class MenuBasedAppUsingAWTDemo extends WindowAdapter {
    Frame f;
    MenuBar b;
    Menu m1,m2;
    MenuItem mi1,mi2,mi3,mi4;
    // constructor
    MenuBasedAppUsingAWTDemo(){
        f= new Frame("Menu Based App With AWT");
        // set size of the frame
        f.setSize(400,300);
        // set layout
        f.setLayout(new FlowLayout());
        // registering the window event
        f.addWindowListener(this);
       
        b=new MenuBar();
         // add menubar to frame
        f.setMenuBar(b);
       
        m1=new Menu("File");
        m2=new Menu("Edit");
       
        // add menu to menubar
        b.add(m1);
        b.add(m2);
       
        mi1=new MenuItem("New");
        mi2=new MenuItem("Edit");
        mi3=new MenuItem("Hello");
        mi4=new MenuItem("Bye");
       
        // add menu items to the menu
        m1.add(mi1);
        m1.add(mi2);
        m2.add(mi3);
        m2.add(mi4);
       
        f.setVisible(true);
       
    }
   
    // main method
    public static void main(String[] args) {
        // call constructor
        new MenuBasedAppUsingAWTDemo();
    }
   
    public void windowClosing(WindowEvent e){
        f.dispose();
    }
   
}
Output:
Outputs are in displayed in two different form i.e. image and video to make you understand. The output image is displayed first and video is displayed at second.

awt menu bar output
.
awt menu bar output

Video Output:


************



Post a Comment

0 Comments