After building the basic window, we will next build the menus that are situated across the top of the window. This is done by, first, creating a
MenuBar
; second, creating the individualMenus
; third, creating the individualMenuItems
for each menu; fourth, adding anActionListener
for each; fifth, adding the menus to the menubar; and, finally, adding the menubar to the window.After the menus are built, we must also add the
ActionListerner
interface to the applet and the required methods for it.For simplicity, the
MyFrame
class will not be included here since it has not changed since Step 1.
Menus
import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class step2 extends Applet implements ActionListener{ // adds menubar and menuitems MyFrame outerBox; Panel centerPanel; Panel bottomPanel; public void init ( ){ buildWindow (); } // end init //****************** buildWindow // *** Global variables for menus and menitems MenuBar menuBar; Menu fileMenu; MenuItem neww, open, close, save, quit; Menu editMenu; MenuItem cut, copy, paste, delete; Menu helpMenu; MenuItem help; private void buildWindow ( ) { // *** Instantiate objects outerBox = new MyFrame ( ); centerPanel = new Panel ( ); bottomPanel = new Panel ( ); // *** Set colors centerPanel.setBackground (Color.white); bottomPanel.setBackground (Color.blue); // *** Build Menus menuBar = new MenuBar ( ); fileMenu = new Menu ("File"); neww = new MenuItem ( "New" ); fileMenu.add ( neww ); neww.addActionListener ( this ); open = new MenuItem ( "Open" ); fileMenu.add ( open ); open.addActionListener ( this ); close = new MenuItem ( "Close" ); fileMenu.add ( close ); close.addActionListener ( this ); save = new MenuItem ( "Save" ); fileMenu.add ( save ); save.addActionListener ( this ); quit = new MenuItem ( "Quit" ); fileMenu.add ( quit ); quit.addActionListener ( this ); editMenu = new Menu ("Edit"); cut = new MenuItem ( "Cut" ); editMenu.add ( cut ); cut.addActionListener ( this ); copy = new MenuItem ( "Copy" ); editMenu.add ( copy ); copy.addActionListener ( this ); paste = new MenuItem ( "Paste" ); editMenu.add ( paste ); paste.addActionListener ( this ); delete = new MenuItem ( "Delete" ); editMenu.add ( delete ); delete.addActionListener ( this ); helpMenu = new Menu ("Help"); help = new MenuItem ( "Help" ); helpMenu.add ( help ); help.addActionListener ( this ); menuBar .add ( fileMenu ); menuBar .add ( editMenu ); menuBar .add ( helpMenu ); menuBar .setHelpMenu ( helpMenu ); // *** build the window outerBox.setMenuBar ( menuBar ); outerBox.setLayout (new BorderLayout ( ) ); outerBox.add ("Center", centerPanel); outerBox.add ("South", bottomPanel); outerBox.resize (600, 500); outerBox.setBackground (Color.white); outerBox.addWindowListener ( outerBox ); outerBox.show( ); } // end buildWindow () //*********** Interface Methods *********** //**** ActionListener methods public void actionPerformed ( ActionEvent e ) { } }// end step2Run the applet