Also available as MenuPlay.java

/**
 * Demo that displays the alphabet.
 *  includes:
 *    creating and using a menubar 
 *       menu allows users to quit, change color, font, ...
 *
 *    a JScrollPane that automatically kicks in when needed
 *      (scrollbars show up whenever  the window is not large enough
 *      to hold a panel's preferred size).
 *     
 */
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import javax.swing.border.*;
import java.io.*;
import java.awt.event.*;

/**
 * MenuPlay is itself an ActionListener, so it can be used 
 * to receive actionEvents
 *
 * All the events received come from the menus, and we just 
 * use the menu item text itself to distinguish which menu item
 * was selected.
 */

public class MenuPlay implements ActionListener {

  /** the panel that holds the alphabet is ldp
   *     LetterDisplayPanel is defined below, it is a special
   *     kind of JPanel.
   */

  LetterDisplayPanel ldp;


  /**
   * main creates a MenuPlay object and tells it to display 
   */
  public static void main(String [] args) {
        
        MenuPlay mp = new MenuPlay();
        mp.showGUI();
  }

  /**
   * showGUI builds the window and initializes the display of the
   * alphabet to some default values
   */

  void showGUI() {
        // build top level JFrame
        JFrame top = new JFrame("Menu Play");

        // set preferred size
        top.setSize(200,200);
        top.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create a panel that holds the alphabet
        ldp = new LetterDisplayPanel();

        // Create a ScrollPane with the letter panel in it.
        JScrollPane jsp = new JScrollPane(ldp);

        // create a menu
        top.setJMenuBar(buildMenu());
        
        // add the letter panel to the window
        top.getContentPane().add(jsp);

        // turn on the window
        top.setVisible(true);
  }


  /**
   * Build the menubar. 
   * there are 4 menus in the menubar, each is built by it's own method.
   */

  JMenuBar buildMenu() {
        JMenuBar menuBar;

        //Create the menu bar.
        menuBar = new JMenuBar();

        //add the File menu
        menuBar.add(FileMenu());

        // add the case menu
        menuBar.add(CaseMenu());

        // add the color menu
        menuBar.add(ColorsMenu());


        // add the font menu
        menuBar.add(FontMenu());
        return(menuBar);
  }

  /**
   * creates the File menu (just a quit menu item)
   */

  public JMenu FileMenu() {
        JMenu menu = new JMenu("File");
        
        JMenuItem menuItem;

        menuItem = new JMenuItem("Quit");
        menuItem.addActionListener(this);
        menu.add(menuItem);
        return(menu);
  }

  /**
   * creates the Case menu 
   *  choices for the display of the alphabet include
   *    UPPER CASE
   *    loweer case
   *    MixED cAsE (random)
   */

  public JMenu CaseMenu() {
        JMenu        menu = new JMenu("Case");

        JMenuItem menuItem;
        //a group of JMenuItems
        menuItem = new JMenuItem("UPPER");
        menuItem.addActionListener(this);
        menu.add(menuItem);

        menuItem = new JMenuItem("lower");
        menuItem.addActionListener(this);
        menu.add(menuItem);

        menuItem = new JMenuItem("MiXeD");
        menuItem.addActionListener(this);
        menu.add(menuItem);
        return(menu);
  }


  /**
   * creates the Color menu 
   *  The color menu changes the color of the letters displayed.
   *  Choices are Black, Blue, Green, Red
   */


  public JMenu ColorsMenu() {
        JMenu menu = new JMenu("Colors");

        JMenuItem menuItem;
        
        menuItem = new JMenuItem("Black");
        menuItem.addActionListener(this);
        menuItem.setForeground(Color.BLACK);
        menu.add(menuItem);


        menuItem = new JMenuItem("Blue");
        menuItem.addActionListener(this);
        menuItem.setForeground(Color.BLUE);
        menu.add(menuItem);

        menuItem = new JMenuItem("Green");
        menuItem.addActionListener(this);
        menuItem.setForeground(Color.GREEN);
        menu.add(menuItem);

        menuItem = new JMenuItem("Red");
        menuItem.addActionListener(this);
        menuItem.setForeground(Color.RED);
        menu.add(menuItem);

        menuItem = new JMenuItem("Random");
        menuItem.addActionListener(this);
        menu.add(menuItem);

        return(menu);
  }

  /**
   * creates the Font menu 
   *   changes the font used to draw the letters.
   *
   *  choices are all logical fonts (to avoid reliance on
   *   specific font names).
   */

  public JMenu FontMenu() {
        JMenu menu = new JMenu("Font");

        JMenuItem menuItem;
        
        menuItem = new JMenuItem("Serif");
        menuItem.addActionListener(this);
        menuItem.setFont(new Font("Serif",Font.BOLD,14));
        menu.add(menuItem);


        menuItem = new JMenuItem("SansSerif");
        menuItem.addActionListener(this);
        menuItem.setFont(new Font("SansSerif",Font.BOLD,14));

        menu.add(menuItem);

        menuItem = new JMenuItem("Monospaced");
        menuItem.addActionListener(this);
        menuItem.setFont(new Font("Monospaced",Font.BOLD,14));
        menu.add(menuItem);

        return(menu);
  }



  /** The method required as an ActionListener.
   * for this demo program, this method receives all the
   * menu events
   */

  public void actionPerformed(ActionEvent e) {

        // find out what the action was
        // (for menu items, this is the item text by default)
        String act = e.getActionCommand();

        if ( act.equals("UPPER") ) {
          ldp.rebuild(LetterDisplayPanel.LDP_UPPER);

        } else if ( act.equals("lower") ) {
          ldp.rebuild(LetterDisplayPanel.LDP_LOWER);

        } else if ( act.equals("MiXeD") ) {
          ldp.rebuild(LetterDisplayPanel.LDP_MIXED);

        } else if (act.equals("Blue")) {
          ldp.changeColor(Color.BLUE);

        } else if (act.equals("Black")) {
          ldp.changeColor(Color.BLACK);

        } else if (act.equals("Red")) {
          ldp.changeColor(Color.RED);

        } else if (act.equals("Green")) {
          ldp.changeColor(Color.GREEN);

        } else if (act.equals("Random")) {
          ldp.changeColorRandom();

        } else if (act.equals("Quit")) {
          System.exit(1);

        } else if (act.equals("Serif")) {
          ldp.rebuild(new Font("Serif",Font.BOLD,14));

        } else if (act.equals("SansSerif")) {
          ldp.rebuild(new Font("SansSerif",Font.BOLD,14));

        } else if (act.equals("Monospaced")) {
          ldp.rebuild(new Font("Monospaced",Font.BOLD,14));
        }
  }
}


/**
 * LetterDisplayPanel displays the alphabet in a grid
 *
 */

class LetterDisplayPanel extends JPanel {

  /** constants used to control what style (case) is used 
   * to display the alphabet
   */

  static final int LDP_UPPER = 0;
  static final  int LDP_LOWER = 1;
  static final int LDP_MIXED = 2;

  /**
   * array of all the letters we need to display 
   */

  static String[] chrs = {
        "A","B","C","D","E","F",
    "G","H","I","J","K","L",
    "M","N","O","P","Q","R",
    "S","T","U","V","W","X",
    "Y","Z"};


  // Random number generator used for generating mixed case alphabet
  Random rand = new Random();

  /**
   * default constructor sets some defaults and builds the 
   * alphabet grid.
   */

  public LetterDisplayPanel() {
        setBackground(Color.GRAY);

        // establish the preferred size
        setPreferredSize(new Dimension(400,300));
        build();
  }

  /**
   * constructor that allows the initial color and font to be set 
   *
   */

  public LetterDisplayPanel(Color c, Font font) {
        build();
  }


  
  /**
   * change the style setting (the case of the letters, upper or lower
   */

  public void rebuild(int style) {
        Component[] clist = getComponents();

        for (int i=0;i<clist.length;i++) {
         ( (JLabel) clist[i]).setText(charString(i,style));
        }
        rebuild();
  }


  /**
   * change the font of each letter in the alphabet 
   */

  public void rebuild(Font f) {

        // get all the components in this panel
        Component[] clist = getComponents();

        // change the font of each one
        for (int i=0;i<clist.length;i++) {
         ( (JLabel) clist[i]).setFont(f);
        }

        // force redisplay
        rebuild();
  }

  /**
   * change the color of each letter of the alphabet
   */
  public void changeColor(Color c) {
        // iterate through and change the color of all the labels
        Component[] clist = getComponents();

        for (int i=0;i<clist.length;i++) {
          clist[i].setForeground(c);
        }

        // force redisplay
        rebuild();
  }

  /** generates a random color.
   */

  Color randomColor() {
        return new Color( rand.nextInt(256),
                                          rand.nextInt(256),
                                          rand.nextInt(256));
  }

  /**
   * change color randomly (each letter a different color) 
   */
  public void changeColorRandom() {

        Component[] clist = getComponents();

        for (int i=0;i<clist.length;i++) {
          clist[i].setForeground(randomColor());
        }

        // force redisplay
        rebuild();
  }


  /**
   * force a layout event and repaint.
   */

  public void rebuild() {
        doLayout();
        invalidate();
        repaint();
  }


  /**
   * generate a string for a letter of the alphabet and
   * given style (case)
   */

  public String charString( int i, int style) {
        switch(style) {
        case LDP_UPPER:
          return chrs[i].toUpperCase();

        case LDP_LOWER:
          return chrs[i].toLowerCase();

        case LDP_MIXED:
                if (rand.nextBoolean()) 
                  return chrs[i].toUpperCase();
                else
                  return chrs[i].toLowerCase();
        default:
          return chrs[i];
        }
  }


  /**
   * build initializes things, creates the 
   *  grid and puts JLabels with the letters of the alphabet
   *  in the grid.
   *
   */

  public void build() {
        setLayout(new GridLayout(5,6,3,3));

        for (int i=0;i<26;i++) {
          JLabel j = new JLabel( charString(i,LDP_UPPER), JLabel.CENTER);
          j.setBorder(BorderFactory.createRaisedBevelBorder());
          j.setFont( new Font("Default",Font.BOLD,14));
          j.setForeground(Color.BLACK);

          // JLabels have a transparent background (by default)
          //  need to change this to have background show up
          j.setOpaque(true);

          j.setBackground(Color.WHITE);
          add(j);
        }
  }

}