Saturday, May 30, 2009

How to create MenuBar Steps

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 of MenuItem or CheckboxMenuItem class for each sub menu item.

MenuItem save =new MenuItem("Save");
MenuItem newf =new MenuItem("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=new MenuBar();
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(new MenuItem("Font"));
options.add(new MenuItem("Size"));
options.add(new MenuItem("Color"));
edit.add(cbm);
cbm.setState(true);
cbm.setEnabled(false);
open.setEnabled(false);
frame.setVisible(true);
frame.setSize(300,300);
}
}

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP