In this section, you will learn about creation of menus, submenus and Separators in Java Swing.
Create menus and submenus in Java
In this section, you will learn about creation of menus, submenus and Separators in Java Swing. Menu bar contains a collection of menus. Each menu can have multiple menu items these are called submenu. Similarly, all menus have multiples menu items. The Separator divides the menu items in a separate groups like same types of menu Items are divided into a individual parts. For pictorial representation, the image for the result of the given program is given below:
This program shows how to create menu bar, menus, submenus and Separators. Here, all items shows on a frame with the help of following methods and APIs:
JMenuBar:
This is the class which constructs a menu bar
that contains several menus.
JMenu(String):
This is the constructor of JMenu class.
This constructor constructs the new menu. It takes the string type value which
is the name label for the menu.
JMenuItem(String):
This is the constructor of JMenuItem
class which constructs new menu items for the specific menu. It takes string types value
which is the label for the menu item.
JSeparator():
This is the constructor of JSeparator class
which adds an extra line between menu items. This line, only separates the menu
items.
setJMenuBar():
This method is used to set the menu bar to the
specified frame. It takes the object of the JMenuBar class.
Here is the code of program:
import javax.swing.*;
|