/** * Name: Stuart Hansen * Course: CSCI 444 * Section: 001 * * Purpose: * This program demonstrates a simple menu system */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Colors extends JFrame { JMenuBar menuBar; // the main menu bar JMenu colorMenu; // the menu containing the color choices JMenu grayMenu; // a submenu containing shades of gray JMenuItem blueMenuItem; // set the background to blue JMenuItem redMenuItem; // set the background to red JMenuItem whiteMenuItem; // set the background to white JMenuItem chooserMenuItem; // use a color chooser to set the background JMenuItem lightGrayMenuItem; // set the background to lightGray JMenuItem grayMenuItem; // set the background to gray JMenuItem darkGrayMenuItem; // set the background to darkGray JPanel panel; // something to put in the frame /** * Constructor for objects of class Colors */ public Colors() { // Setup the menu bar menuBar = new JMenuBar(); setJMenuBar(menuBar); // Set up the color menu colorMenu = new JMenu("Color"); menuBar.add(colorMenu); // Set up the blue menu item blueMenuItem = new JMenuItem("Blue"); blueMenuItem.addActionListener(new menuHandler(Color.blue)); colorMenu.add(blueMenuItem); // Set up the red menu item redMenuItem = new JMenuItem("Red"); redMenuItem.addActionListener(new menuHandler(Color.red)); colorMenu.add(redMenuItem); // Set up the white menu item whiteMenuItem = new JMenuItem("White"); whiteMenuItem.addActionListener(new menuHandler(Color.white)); colorMenu.add(whiteMenuItem); // Set up the gray menu grayMenu = new JMenu("Gray"); colorMenu.add(grayMenu); // Set up the light gray menu item lightGrayMenuItem = new JMenuItem("Light Gray"); lightGrayMenuItem.addActionListener(new menuHandler(Color.lightGray)); grayMenu.add(lightGrayMenuItem); // Set up the gray menu item grayMenuItem = new JMenuItem("Gray"); grayMenuItem.addActionListener(new menuHandler(Color.gray)); grayMenu.add(grayMenuItem); // Set up the dark gray menu item darkGrayMenuItem = new JMenuItem("Dark Gray"); darkGrayMenuItem.addActionListener(new menuHandler(Color.darkGray)); grayMenu.add(darkGrayMenuItem); // Set up the colorChooser menu item chooserMenuItem = new JMenuItem("Color Chooser"); chooserMenuItem.addActionListener(new ColorChooserHandler()); colorMenu.add(chooserMenuItem); // add a panel to the content pane Container c = getContentPane(); panel = new JPanel(); panel.setBackground(Color.white); c.add(panel); // take care of the windowing stuff setSize(300, 300); setLocation(250, 250); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } // This handler class takes care of almost all the menu items // except for the color chooser option. class menuHandler implements ActionListener { Color color; public menuHandler (Color color) { this.color = color; } public void actionPerformed (ActionEvent e) { panel.setBackground(color); } } // This handler fires up a color chooser to select the new color class ColorChooserHandler implements ActionListener { public void actionPerformed (ActionEvent e) { // We save the old color in case the user cancels Color oldColor = panel.getBackground(); Color newColor; newColor = JColorChooser.showDialog(panel, "Choose a new color", oldColor); if (newColor != null) panel.setBackground(newColor); } } // The main starts it all running public static void main (String [] args) { new Colors(); } }