import java.awt.event.*; import javax.swing.*; // this class contains the method called when the exit choice is picked class ExitListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } } public class MenuExample { public static void main(String[] args) { JFrame frame = new JFrame("Menu Example!"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // create the menu bar JMenuBar menubar = new JMenuBar(); // add the file menu JMenu file = new JMenu("File"); file.setMnemonic(KeyEvent.VK_F); // add a menu item JMenuItem exit = new JMenuItem("Exit", null); exit.setMnemonic(KeyEvent.VK_E); exit.setToolTipText("Exit the program"); // add the action as a new anonymous object exit.addActionListener(new ExitListener()); file.add(exit); // add file to the menubar menubar.add(file); // add the menubar to the window frame.setJMenuBar(menubar); // set other things frame.setTitle("Simple menu"); frame.setSize(300, 200); // launch the windwo frame.setVisible(true); } }