We can also develop an application with a Menu. As a name indicates a Menu consists of Menu objects. These Menu objects comprise of MenuItem objects which can be selected by the user with a click of a mouse. A MenuItem may be a String, checkbox, separator, menu etc.
Following are the steps to to add menus to any Frame:
MenuBar mb = new MenuBar();
2. Then you need to create a Menu using Menu m = new Menu("File");.
3. Now the MenuItem options can be added to the Menu from top to bottom, using the following methods.
mi.add(new MenuItem("Open"));
mi.add(new
CheckboxMenuItem("Type here"));
4. Now you can add the Menu to the MenuBar from left to right using mi.add(m);.
5. Finally, you need
to add the MenuBar to the Frame by calling the setMenuBar() method.
The program code given below, creates an application window with a menu bar.
|
Output of the program:
| C:\newprgrm>javac MainWindow.java C:\newprgrm>java MainWindow |

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Menus View All Comments
Post your Comment