1.4 AWT Controls & Layout Manager Part-3
1.4 AWT Controls & Layout Manager Part-3
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
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(); } }