JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); menuBar.add(fileMenu);
JMenu editMenu = new JMenu("Edit"); JMenuItem cutMenuItem = new JMenuItem("Cut"); JMenuItem copyMenuItem = new JMenuItem("Copy"); JMenuItem pasteMenuItem = new JMenuItem("Paste"); editMenu.add(cutMenuItem); editMenu.add(copyMenuItem); editMenu.add(pasteMenuItem);
JMenu helpMenu = new JMenu("Help"); JMenuItem aboutMenuItem = new JMenuItem("About"); helpMenu.add(aboutMenuItem);This code creates a JMenu named "Help" and a single JMenuItem named "About". The JMenuItem is then added to the menu using the add() method. Overall, the JMenu class is a powerful component in the javax.swing library that allows for the creation of menus in a GUI application.