/* MenuTest.java * * Bruce M. Bolden * March 5, 1998 */ import java.awt.*; import java.awt.event.*; import java.util.Random; import corejava.*; public class MenuTest extends CloseableFrame implements ActionListener { protected Color textColor = Color.magenta; public static void main(String args[]) { MenuTest f = new MenuTest(); InitializeMenu( f ); f.setSize( 300, 300 ); f.show(); } /** Event handler */ public void actionPerformed( ActionEvent e ) { Color oldColor = textColor; String cmd = e.getActionCommand(); if( cmd.equals("clear") ) // File menu clear(); else if( cmd.equals("quit") ) System.exit(0); else if( cmd.equals("undo") ) // Edit menu /* do nothing */ ; else if( cmd.equals("cut") ) /* do nothing */ ; else if( cmd.equals("copy") ) /* do nothing */ ; else if( cmd.equals("paste") ) /* do nothing */ ; else if( cmd.equals("red") ) // Color menu textColor = Color.red ; else if( cmd.equals("green") ) textColor = Color.green ; else if( cmd.equals("blue") ) textColor = Color.blue ; if( oldColor != textColor ) repaint(); // Note: calls paint() indirectly! } /** Clear the graphics context. */ protected void clear() { Graphics g = this.getGraphics(); g.setColor( this.getBackground() ); g.fillRect( 0, 0, this.getSize().width, this.getSize().height ); } public void paint( Graphics g ) { Random r = new Random(); for( int i = 0 ; i < 100 ; i++ ) { int x = r.nextInt() % this.getSize().width; int y = r.nextInt() % this.getSize().height; g.setColor( textColor ); g.drawString( "Hello!", x, y ); } } /** Initialize the menu */ protected static void InitializeMenu( MenuTest f ) { MenuBar mBar = new MenuBar(); f.setMenuBar( mBar ); // Create/Add menus to Menu bar Menu file = new Menu( "File" ); Menu edit = new Menu( "Edit" ); Menu color = new Menu( "Color" ); mBar.add( file ); mBar.add( edit ); mBar.add( color ); // Create and add menu items to menus // "File" menu MenuItem clear = new MenuItem( "Clear", new MenuShortcut(KeyEvent.VK_C) ); clear.addActionListener(f); clear.setActionCommand( "clear" ); file.add(clear); MenuItem quit = new MenuItem( "Quit", new MenuShortcut(KeyEvent.VK_Q) ); quit.addActionListener(f); quit.setActionCommand( "quit" ); file.add(quit); // "Edit" menu /* MenuItem undo = new MenuItem( "Undo", new MenuShortcut(KeyEvent.VK_Z) ); undo.addActionListener(f); undo.setActionCommand( "undo" ); edit.add(undo); .... MenuItem paste = new MenuItem( "Paste", new MenuShortcut(KeyEvent.VK_V) ); paste.addActionListener(f); paste.setActionCommand( "paste" ); edit.add(paste); */ createMenuItem( edit, f, "Undo", "undo", KeyEvent.VK_Z ); createMenuItem( edit, f, "Cut", "cut", KeyEvent.VK_X ); createMenuItem( edit, f, "Copy", "copy", KeyEvent.VK_C ); createMenuItem( edit, f, "Paste", "paste", KeyEvent.VK_V ); // "Color" menu createMenuItem( color, f, "Red", "red", KeyEvent.VK_R ); createMenuItem( color, f, "Green", "green", KeyEvent.VK_G ); createMenuItem( color, f, "Blue", "blue", KeyEvent.VK_B ); } /** A utility function for creating and adding menu items to a menu. */ protected static void createMenuItem( Menu m, ActionListener listener, String label, String cmdString, int shortcut ) { MenuItem mi = new MenuItem( label, new MenuShortcut(shortcut) ); mi.addActionListener(listener); mi.setActionCommand( cmdString ); m.add(mi); } }