import javax.swing.*; import java.awt.*; import javax.swing.event.*; import java.awt.event.*; public class Sample2 extends JFrame{ // inherit JFrame /** * This Constructor sets up the window and creates a simple menu */ public Sample2() { // // setup the frames banner/title bar // super("Sample 2"); // call JFrame's constructor and give it a title setSize(300,200); // set the width of the window to 300 pixels, height to 200. // setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // exit the whole program when this frame is closed addWindowListener(new WindowAdapter() { // Quit the application public void windowClosing(WindowEvent e) { System.exit(0); } }); // // Create a simple menu // JMenuBar mainMenu = new JMenuBar(); // create a menubar JMenu fileMenu = new JMenu("File"); // Create the file menu fileMenu.setMnemonic(KeyEvent.VK_F); // set its Alt character sequence addMenuItem(fileMenu, "New", KeyEvent.VK_N, KeyEvent.VK_N,KeyEvent.CTRL_MASK,new FileNewMenuListener()); addMenuItem(fileMenu, "Open", KeyEvent.VK_O, KeyEvent.VK_O,KeyEvent.CTRL_MASK,new FileOpenMenuListener()); fileMenu.addSeparator(); // add a separator between open and exit addMenuItem(fileMenu, "Exit", KeyEvent.VK_X,new FileExitMenuListener()); JMenu editMenu = new JMenu("Edit"); // create the edit menu editMenu.setMnemonic(KeyEvent.VK_E); addMenuItem(editMenu, "Cut", KeyEvent.VK_T, KeyEvent.VK_Q,KeyEvent.CTRL_MASK,new EditCutMenuListener()); addMenuItem(editMenu, "Copy", KeyEvent.VK_C, KeyEvent.VK_C,KeyEvent.CTRL_MASK,new EditCopyMenuListener()); addMenuItem(editMenu, "Paste", KeyEvent.VK_P, KeyEvent.VK_V,KeyEvent.CTRL_MASK,new EditPasteMenuListener()); mainMenu.add(fileMenu); // add the file menu to the main menu bar mainMenu.add(editMenu); // add the edit menu to the main menu bar // // Add it to the frame // setJMenuBar(mainMenu); // tell the frame to use this menu. } /** * This routine creates and adds a menu item to a menu * @param menu reference to a JMenu object to add this item to * @param text text string to appear on the menu * @param mnemonic virtual key to use as the mnemonic for the item e.g. KeyEvent.VK_F * @param acceleratorKey virtual key to use for the keyboard accelerator, e.g. KeyEvent.VK_O * @param acceleratorModifier virtual mask to use as a modifier, eg. KeyEvent.CTRL_MASK * @param listener reference to an object that implements an ActionListener */ void addMenuItem(JMenu menu, String text, int mnemonic, int acceleratorKey, int acceleratorModifier, ActionListener listener) { JMenuItem anItem; anItem = new JMenuItem(text,mnemonic); // create a menu item. anItem.setAccelerator(KeyStroke.getKeyStroke(acceleratorKey,acceleratorModifier)); // define accelerator for open anItem.addActionListener(listener); // tell menuitem what object will listen for it. menu.add(anItem); // add the item to the menu } /** * This routine creates and adds a menu item to a menu, no accelerator * @param menu reference to a JMenu object to add this item to * @param text text string to appear on the menu * @param mnemonic virtual key to use as the mnemonic for the item e.g. KeyEvent.VK_F * @param listener reference to an object that implements an ActionListener */ void addMenuItem(JMenu menu, String text, int mnemonic,ActionListener listener) { JMenuItem anItem; anItem = new JMenuItem(text,mnemonic); // create a menu item. anItem.addActionListener(listener); // tell menuitem what object will listen for it. menu.add(anItem); // add the item to the menu } /********************************* * Main routine, Note, this routine can be in any class. */ public static void main(String[] arguments){ Sample2 fr = new Sample2(); // create the sample 2 frame fr.setVisible(true); // allow it to be seen } /******************************************************************* * Inner Classes, one acts as a listener for each user interface item * * Alternatively you could just implement one actionPerformed routine * in the Frame class, however, that routine would have to do a lot of * work just to figure out what menu item, button, etc, created the event. * this is easier, cleaner, and isolates code for each UI item/ */ /** * Handle the File-Exit menu item */ class FileExitMenuListener implements ActionListener{ public void actionPerformed(ActionEvent event) { System.exit(0); } } /** * Handle the File-New menu item */ class FileNewMenuListener implements ActionListener{ public void actionPerformed(ActionEvent event) { System.out.println("File-new selected"); } } /** * Handle the File-Open menu item */ class FileOpenMenuListener implements ActionListener{ public void actionPerformed(ActionEvent event) { System.out.println("File-open selected"); } } /** * Handle the Edit-Cut menu item */ class EditCutMenuListener implements ActionListener{ public void actionPerformed(ActionEvent event) { System.out.println("Edit - Cut selected"); } } /** * Handle the Edit-Copy menu item */ class EditCopyMenuListener implements ActionListener{ public void actionPerformed(ActionEvent event) { System.out.println("Edit - Copy selected"); } } /** * Handle the Edit-paste menu item */ class EditPasteMenuListener implements ActionListener{ public void actionPerformed(ActionEvent event) { System.out.println("Edit Paste selected"); } } /** * Handle the Help Topics menu item */ class HelpTopicsMenuListener implements ActionListener{ public void actionPerformed(ActionEvent event) { System.out.println("Help topics was selected"); } } }