Source of MenuFrame.java


  1: // Fig. 22.5: MenuFrame.java
  2: // Demonstrating menus.
  3: import java.awt.Color;
  4: import java.awt.Font;
  5: import java.awt.BorderLayout;
  6: import java.awt.event.ActionListener;
  7: import java.awt.event.ActionEvent;
  8: import java.awt.event.ItemListener;
  9: import java.awt.event.ItemEvent;
 10: import javax.swing.JFrame;
 11: import javax.swing.JRadioButtonMenuItem;
 12: import javax.swing.JCheckBoxMenuItem;
 13: import javax.swing.JOptionPane;
 14: import javax.swing.JLabel;
 15: import javax.swing.SwingConstants;
 16: import javax.swing.ButtonGroup;
 17: import javax.swing.JMenu;
 18: import javax.swing.JMenuItem;
 19: import javax.swing.JMenuBar;
 20: 
 21: public class MenuFrame extends JFrame 
 22: {
 23:    private final Color colorValues[] = 
 24:       { Color.BLACK, Color.BLUE, Color.RED, Color.GREEN };   
 25:    private JRadioButtonMenuItem colorItems[]; // color menu items
 26:    private JRadioButtonMenuItem fonts[]; // font menu items
 27:    private JCheckBoxMenuItem styleItems[]; // font style menu items
 28:    private JLabel displayJLabel; // displays sample text
 29:    private ButtonGroup fontButtonGroup; // manages font menu items
 30:    private ButtonGroup colorButtonGroup; // manages color menu items
 31:    private int style; // used to create style for font
 32: 
 33:    // no-argument constructor set up GUI
 34:    public MenuFrame()
 35:    {
 36:       super( "Using JMenus" );     
 37: 
 38:       JMenu fileMenu = new JMenu( "File" ); // create file menu
 39:       fileMenu.setMnemonic( 'F' ); // set mnemonic to F
 40: 
 41:       // create About... menu item
 42:       JMenuItem aboutItem = new JMenuItem( "About..." );
 43:       aboutItem.setMnemonic( 'A' ); // set mnemonic to A
 44:       fileMenu.add( aboutItem ); // add about item to file menu
 45:       aboutItem.addActionListener(
 46: 
 47:          new ActionListener() // anonymous inner class
 48:          {  
 49:             // display message dialog when user selects About...
 50:             public void actionPerformed( ActionEvent event )
 51:             {
 52:                JOptionPane.showMessageDialog( MenuFrame.this,
 53:                   "This is an example\nof using menus",
 54:                   "About", JOptionPane.PLAIN_MESSAGE );
 55:             } // end method actionPerformed
 56:          } // end anonymous inner class
 57:       ); // end call to addActionListener
 58:  
 59:       JMenuItem exitItem = new JMenuItem( "Exit" ); // create exit item
 60:       exitItem.setMnemonic( 'x' ); // set mnemonic to x
 61:       fileMenu.add( exitItem ); // add exit item to file menu
 62:       exitItem.addActionListener(
 63: 
 64:          new ActionListener() // anonymous inner class
 65:          {  
 66:             // terminate application when user clicks exitItem
 67:             public void actionPerformed( ActionEvent event )
 68:             {
 69:                System.exit( 0 ); // exit application
 70:             } // end method actionPerformed
 71:          } // end anonymous inner class
 72:       ); // end call to addActionListener
 73: 
 74:       JMenuBar bar = new JMenuBar(); // create menu bar
 75:       setJMenuBar( bar ); // add menu bar to application
 76:       bar.add( fileMenu ); // add file menu to menu bar
 77: 
 78:       JMenu formatMenu = new JMenu( "Format" ); // create format menu
 79:       formatMenu.setMnemonic( 'r' ); // set mnemonic to r
 80: 
 81:       // array listing string colors
 82:       String colors[] = { "Black", "Blue", "Red", "Green" };
 83: 
 84:       JMenu colorMenu = new JMenu( "Color" ); // create color menu
 85:       colorMenu.setMnemonic( 'C' ); // set mnemonic to C
 86: 
 87:       // create radiobutton menu items for colors
 88:       colorItems = new JRadioButtonMenuItem[ colors.length ];
 89:       colorButtonGroup = new ButtonGroup(); // manages colors
 90:       ItemHandler itemHandler = new ItemHandler(); // handler for colors
 91: 
 92:       // create color radio button menu items
 93:       for ( int count = 0; count < colors.length; count++ ) 
 94:       {
 95:          colorItems[ count ] = 
 96:             new JRadioButtonMenuItem( colors[ count ] ); // create item
 97:          colorMenu.add( colorItems[ count ] ); // add item to color menu
 98:          colorButtonGroup.add( colorItems[ count ] ); // add to group
 99:          colorItems[ count ].addActionListener( itemHandler );
100:       } // end for
101: 
102:       colorItems[ 0 ].setSelected( true ); // select first Color item
103: 
104:       formatMenu.add( colorMenu ); // add color menu to format menu
105:       formatMenu.addSeparator(); // add separator in menu
106: 
107:       // array listing font names
108:       String fontNames[] = { "Serif", "Monospaced", "SansSerif" };
109:       JMenu fontMenu = new JMenu( "Font" ); // create font menu
110:       fontMenu.setMnemonic( 'n' ); // set mnemonic to n
111: 
112:       // create radiobutton menu items for font names
113:       fonts = new JRadioButtonMenuItem[ fontNames.length ];
114:       fontButtonGroup = new ButtonGroup(); // manages font names
115: 
116:       // create Font radio button menu items
117:       for ( int count = 0; count < fonts.length; count++ ) 
118:       {
119:          fonts[ count ] = new JRadioButtonMenuItem( fontNames[ count ] );
120:          fontMenu.add( fonts[ count ] ); // add font to font menu
121:          fontButtonGroup.add( fonts[ count ] ); // add to button group
122:          fonts[ count ].addActionListener( itemHandler ); // add handler
123:       } // end for
124: 
125:       fonts[ 0 ].setSelected( true ); // select first Font menu item
126:       fontMenu.addSeparator(); // add separator bar to font menu
127: 
128:       String styleNames[] = { "Bold", "Italic" }; // names of styles
129:       styleItems = new JCheckBoxMenuItem[ styleNames.length ];
130:       StyleHandler styleHandler = new StyleHandler(); // style handler
131: 
132:       // create style checkbox menu items
133:       for ( int count = 0; count < styleNames.length; count++ ) 
134:       {
135:          styleItems[ count ] = 
136:             new JCheckBoxMenuItem( styleNames[ count ] ); // for style
137:          fontMenu.add( styleItems[ count ] ); // add to font menu
138:          styleItems[ count ].addItemListener( styleHandler ); // handler
139:       } // end for
140: 
141:       formatMenu.add( fontMenu ); // add Font menu to Format menu
142:       bar.add( formatMenu ); // add Format menu to menu bar
143:      
144:       // set up label to display text
145:       displayJLabel = new JLabel( "Sample Text", SwingConstants.CENTER );
146:       displayJLabel.setForeground( colorValues[ 0 ] );
147:       displayJLabel.setFont( new Font( "Serif", Font.PLAIN, 72 ) );
148: 
149:       getContentPane().setBackground( Color.CYAN ); // set background
150:       add( displayJLabel, BorderLayout.CENTER ); // add displayJLabel
151:    } // end MenuFrame constructor
152: 
153:    // inner class to handle action events from menu items
154:    private class ItemHandler implements ActionListener 
155:    {
156:       // process color and font selections
157:       public void actionPerformed( ActionEvent event )
158:       {
159:          // process color selection
160:          for ( int count = 0; count < colorItems.length; count++ )
161:          {
162:             if ( colorItems[ count ].isSelected() ) 
163:             {
164:                displayJLabel.setForeground( colorValues[ count ] );
165:                break;
166:             } // end if
167:          } // end for
168: 
169:          // process font selection
170:          for ( int count = 0; count < fonts.length; count++ )
171:          {
172:             if ( event.getSource() == fonts[ count ] ) 
173:             {
174:                displayJLabel.setFont( 
175:                   new Font( fonts[ count ].getText(), style, 72 ) );
176:             } // end if
177:          } // end for
178: 
179:          repaint(); // redraw application
180:       } // end method actionPerformed
181:    } // end class ItemHandler
182: 
183:    // inner class to handle item events from check box menu items
184:    private class StyleHandler implements ItemListener 
185:    {
186:       // process font style selections
187:       public void itemStateChanged( ItemEvent e )
188:       {
189:          style = 0; // initialize style
190: 
191:          // check for bold selection
192:          if ( styleItems[ 0 ].isSelected() )
193:             style += Font.BOLD; // add bold to style
194: 
195:          // check for italic selection
196:          if ( styleItems[ 1 ].isSelected() )
197:             style += Font.ITALIC; // add italic to style
198: 
199:          displayJLabel.setFont( 
200:             new Font( displayJLabel.getFont().getName(), style, 72 ) );
201:          repaint(); // redraw application
202:       } // end method itemStateChanged
203:    } // end class StyleHandler
204: } // end class MenuFrame
205: 
206: 
207: /**************************************************************************
208:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
209:  * Pearson Education, Inc. All Rights Reserved.                           *
210:  *                                                                        *
211:  * DISCLAIMER: The authors and publisher of this book have used their     *
212:  * best efforts in preparing the book. These efforts include the          *
213:  * development, research, and testing of the theories and programs        *
214:  * to determine their effectiveness. The authors and publisher make       *
215:  * no warranty of any kind, expressed or implied, with regard to these    *
216:  * programs or to the documentation contained in these books. The authors *
217:  * and publisher shall not be liable in any event for incidental or       *
218:  * consequential damages in connection with, or arising out of, the       *
219:  * furnishing, performance, or use of these programs.                     *
220:  *************************************************************************/
221: