You can create menu by using Java code or by using Java swing menu controls. The way of creating Menu Bar and Menu items by using Java Swing Menus control has been shown in this tutorial. Follow the steps properly to create a menu with basic menu items.
Step-1:
Create a Java project named JavaMenu without any class.
Step-2:
Add a JFrame in the project to add the Menu.
Step-3:
Add a menu bar in the form by using Menu Bar control from Swing Menus.
Step-4:
Create the shortcut key to access the File menu by using nmemonic property. ‘F’ character is used here.
Step-5:
Right click on the File menu and select Menu Item to add new menu item for the File menu. Next, rename the menu item to New. Similar way, add Open and Save menu items.
Step-6:
Double click on the shortcut option of each menu item and add the shortcut key by using Accelerator window.
Step-7:
The menu will be look like the following images after adding the menu items with the shortcut keys.
File Menu:
Edit Menu:
Format Menu:
Step-8:
Add separator in the File Menu after adding three menu items.
Step-9:
File menu will be looked like the following image after adding separator and another menu item.
Step-10:
Set the following property of JFrame after selecting Code tab to display the window in the center of the screen.
Step-11:
Run the application. The window with the menu will be displayed in the center of the screen.