0% found this document useful (0 votes)
6 views

1.4 AWT Controls & Layout Manager Part-3

Uploaded by

nihalpatil0021
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

1.4 AWT Controls & Layout Manager Part-3

Uploaded by

nihalpatil0021
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Menubar and Menu

Menubar class-
- A menu bar can be created using MenuBar class.
-A menu bar may contain one or multiple menus, and these menus are created using Menu class.
Constructor-
Menubar()- it creates just menubar

setMenuBar(Menubar obj)-use to set the menubar on frame


Menu-
-Individual menu are of type Menu
constructors-
1. Menu()-creates empty menu
2.Menu(String itemName)-creates menu with specified string
3. Menu(String itemName, Boolean removable)-creates menu with removable option
MenuItem-
-Individual menu items are of type MenuItem
constructors-
1. MenuItem( )-creates empty menuitem
2.MenuItem(String itemName)creates menuitem with specified string
3. MenuItem(String itemName, MenuShortcut keyAccel)-creates menuitem with shortcut key
Methods-
1.void setEnabled(boolean enabledFlag)-enable a menu item
2. boolean isEnabled( )-disable a menu item
3. void setLabel(String newName)-change the name of a menu item
4. String getLabel( )-retrieve the current name

Once you have created a menu item, you must add the item to a Menu object by using add( ), which has
the following general form:
MenuItem add(MenuItem item)
Once you have added all items to a Menu object, you can add that object to the menu bar by using this
version of add( ) defined by MenuBar:
Menu add(Menu menu)
CheckboxMenuItem-
represents a check box which can be included in a menu. Selecting the check box in the menu changes
control's state from on to off or from off to on.

Constructor-
1.CheckboxMenuItem()-Create a check box menu item with an empty label.
2. CheckboxMenuItem(String label)-Create a check box menu item with the specified label.
3. CheckboxMenuItem(String label, boolean state)-Create a check box menu item with the specified label
and state.
Program- Develop a program to add menu on frame
import java.awt.*;
class MenuExample1
{
MenuExample1(){
Frame f= new Frame("Menu and MenuItem Example");
MenuBar mb=new MenuBar();
Menu menu=new Menu("File");
MenuItem i1=new MenuItem("New");
MenuItem i2=new MenuItem("Save");
menu.add(i1);
menu.add(i2);
mb.add(menu);
f.setMenuBar(mb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new MenuExample1();
}
}
Program- Develop a program to add menu on frame
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(); } }

You might also like