This example program shows how to add a Menu Bar to JFrame in Swing. Below Java code create and set JMenu, JMenuItem and JMenuBar components of JFrame in Swing UI.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
import javax.swing.JMenuBar; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JFrame; import javax.swing.SwingConstants; public class AddMenuBarIntoJFrame { public static void main(String[] args) { //Create a menu bar JMenuBar frameMenuBar = new JMenuBar(); //Create first menu in menubar JMenu menu1 = new JMenu("Menu1"); //Create second menu in menubar JMenu menu2 = new JMenu("Menu2"); //Create third menu in menubar JMenu menu3 = new JMenu("Menu3"); //Create menu that will be add into third menu JMenu menu3_internalMenu1 = new JMenu("Menu3_Menu1"); //Create first menu item for first menu JMenuItem menuItem1 = new JMenuItem("Menu1_MenuItem1"); //Create first menu item for second menu JMenuItem menuItem2 = new JMenuItem("Menu2_MenuItem1"); //Create second menu item for second menu JMenuItem menuItem3 = new JMenuItem("Menu2_MenuItem2"); //Create first menu item for third menu JMenuItem menuItem4 = new JMenuItem("Menu3_MenuItem1"); //Create menu item that will be add into internal menu in third menu JMenuItem menuItem5 = new JMenuItem("Menu3_Menu1_MenuItem1"); //add first menu item into first menu menu1.add(menuItem1); //add first menu item into second menu menu2.add(menuItem2); //add second menu item into second menu menu2.add(menuItem3); //add first menu item into third menu menu3.add(menuItem4); //add internal menu into third menu menu3.add(menu3_internalMenu1); //add menu item into internal menu menu3_internalMenu1.add(menuItem5); //add first menu into menu bar frameMenuBar.add(menu1); //add second menu into menu bar frameMenuBar.add(menu2); //add third menu into menu bar frameMenuBar.add(menu3); //Create a JFrame with title ( JFrame with menu bar ) JFrame frame = new JFrame("JFrame with menu bar"); //Set menu bar for JFrame frame.setJMenuBar(frameMenuBar); //Set default close operation for JFrame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set JFrame size frame.setSize(400, 100); //Make JFrame visible. So we can see it. frame.setVisible(true); } } |