Thursday, July 8, 2010

How to create MenuBar Steps  

0 comments

How to create Menu and MenuBars Step wise procedure

1.Create an object of MenuBar class:MenuBar can be attached a Frame and it will be displayed just below the title bar.
MenuBar mb=new MenuBar();

2.Call the method setMenuBar(MenuBar):-This method is used to attach the MenuBar to a Frame.
setMenuBar(mb);

3.Create objects of the Menu class for each menu you want to add on menu bar.

Menu file=new Menu("File");
Menu edit=new Menu("Edit");
Menu help=new Menu("Help");

4.Call add() method of MenuBar class to add each menu object to the menu.

mb.add(file);
mb.add(edit);
mb.add(help);

5.Create object ofMenuItem orCheckboxMenuItem class for each sub menu item.

MenuItem save =newMenuItem("Save");
MenuItem newf =newMenuItem("New");
MenuItem open =new MenuItem("Open");
CheckboxMenuItem cbm=new CheckboxMenuItem("check");

6.Call add() method of Menu class to add each menu item to its appropriate menu.

file.add(newf);
file.add(open);
file.add(save);

Complete Example :-

Source Code:-
//MyMenubar.java


import java.awt.*;
public class MyMenubar
{
public static void main(String a[])
{
Frame frame=new Frame("MenuBar Demo");
MenuBar mb=newMenuBar();
frame.setMenuBar(mb);
Menu file=new Menu("File");
Menu edit=new Menu("Edit");
Menu help=new Menu("Help");
Menu options=new Menu("Options");
mb.add(file);
mb.add(edit);
mb.add(help);
MenuItem save =new MenuItem("Save");
MenuItem newf =new MenuItem("New");
MenuItem open =new MenuItem("Open");
CheckboxMenuItem cbm=new CheckboxMenuItem("check");
file.add(newf);
file.add(open);
file.add(save);
file.add(options);
options.add(newMenuItem("Font"));
options.add(newMenuItem("Size"));
options.add(newMenuItem("Color"));
edit.add(cbm);
cbm.setState(true);
cbm.setEnabled(false);
open.setEnabled(false);
frame.setVisible(true);
frame.setSize(300,300);
}
}

What next?

You can also bookmark this post using your favorite bookmarking service:

Related Posts by Categories



Extension Factory Builder