Explain creation of menu bar, menu, and menu items with suitable java code segment.
MENUBARS
A top-level window can have a menu bar associated with it. A menu bar displays a list of top level menu choices. Each choice is associated with a drop-down menu.
Commonly used Constructors of MenuBar Class
MenuBar(); //Creates a new menu bar.
Commonly used Public Methods of MenuBar Class
Menu add(Menu m); //Adds the specified menu to the menu bar.
Menu getMenu(int i); //Gets the specified menu. (an
int getMenuCount(); //Gets the number of menus on the menu bar.
void remove(int index); //Removes the menu located at the specified index from this menu bar.
void remove(MenuComponent m); //Removes the specified menu component from this menu bar.
Menu Class
The Menu class represents the pull-down menu component that is deployed from a menu bar. We can create a hierarchy of submenus within a menu.
Commonly used Constructors of Menu Class
Menu(); //Constructs a new menu with an empty label.
Menu(String label); //Constructs a new menu with the specified label.
Menu(String label, boolean tear of); //Constructs a new menu with the specified label, indicating whether the menu can be torn off.
Commonly used Public Methods of Menu Class
Menultem add(Menultem mi); //Adds the specified menu item to this menu. void addSeparator(); //Adds a separator line, or a hyphen, to the menu at the current position.
Menultem getItem(int index); //Gets the item located at the specified index of this menu.
int getItemCount(); //Get the number of items in this menu.
void insert(MenuItem menuitem, int index); //Inserts a menu item into this menu at the specified position.
void insertSeparator(int index); //Inserts a separator at the specified position.
void remove(int index); //Removes the menu item at the specified index from this menu.
void remove(MenuComponent item); //Removes the specified menu item from this menu.
void removeAll(); //Removes all items from this menu.
MenuItem Class
All items in a menu should derive from class Menultem or one of its subclasses. By default, it embodies a simple labeled menu item.
Commonly used Constructors of Menultem Class
MenuItem(); //Constructs a new MenuItem with an empty label and no keyboard shortcut. MenuItem(String label); //Constructs a new Menultem with the specified label and no keyboard shortcut.
MenuItem(String label, MenuShortcut s); //Create a menu item with an associated keyboard shortcut.
Commonly used Public Methods of Menultem Class
void deleteShortcut(); //Delete any MenuShortcut object associated with this menu item.
String getAction Command(); //Gets the command name of the action event that is fired by this menu item.
OR
Java AWT MenuItem and Menu
The object of MenuItem class adds a simple labeled menu item on the menu. The items used in a menu must belong to the MenuItem or any of its subclass.
The object of the Menu class is a pull-down menu component that is displayed on the menu bar. It inherits the MenuItem class.
AWT MenuItem class declaration
public class MenuItem extends MenuComponent implements Accessible
AWT Menu class declaration
public class Menu extends MenuItem implements MenuContainer, Accessible
Java AWT MenuItem and Menu Example
import java.awt.*;
class MenuExample
{
MenuExample(){
Frame f= new Frame("Menu and MenuItem Example");
MenuBar mb=new MenuBar();
Menu menu=new Menu("Menu");
Menu submenu=new Menu("Sub Menu");
MenuItem i1=new MenuItem("Item 1");
MenuItem i2=new MenuItem("Item 2");
MenuItem i3=new MenuItem("Item 3");
MenuItem i4=new MenuItem("Item 4");
MenuItem i5=new MenuItem("Item 5");
menu.add(i1);
menu.add(i2);
menu.add(i3);
submenu.add(i4);
submenu.add(i5);
menu.add(submenu);
mb.add(menu);
f.setMenuBar(mb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new MenuExample();
}
}
Output:
Comments
Post a Comment