Illustrates the insertion of menu entries in Frame menu bars. #Programming Code Examples #Java/J2EE/J2ME #AWT Components

**************
ColorMenu.java 
**************
import java.awt.*;
import java.awt.event.*;

/** Illustrates the insertion of menu entries in Frame
 *  menu bars.
 *
 
public class ColorMenu extends CloseableFrame 
                       implements ActionListener {
                        
  private String[] colorNames =
    { "Black", "White", "Light Gray", "Medium Gray", 
      "Dark Gray" };
  private Color[] colorValues =
    { Color.black, Color.white, Color.lightGray, 
      Color.gray, Color.darkGray };
   
  public ColorMenu() {
    super("ColorMenu");
    MenuBar bar = new MenuBar();
    Menu colorMenu = new Menu("Colors");
    for(int i=0; i<2; i++) {
      colorMenu.add(colorNames[i]);
    }
    Menu grayMenu = new Menu("Gray");
    for(int i=2; i<colornames .length; i++) {
      grayMenu.add(colorNames[i]);
    }
    colorMenu.add(grayMenu);
    bar.add(colorMenu);
    setMenuBar(bar);
    colorMenu.addActionListener(this);
    grayMenu.addActionListener(this);
    
    setBackground(Color.lightGray);
    setSize(400, 200);
    setVisible(true);
  }

  /** Catch menu events in the containing Frame. */
  
  public void actionPerformed(ActionEvent event) {
      setBackground(colorNamed(event.getActionCommand()));
      repaint();
  }

  private Color colorNamed(String colorName) {
    for(int i=0; i<colorNames.length; i++) {
      if(colorNames[i].equals(colorName)) {
        return(colorValues[i]);
      }
    }
    return(Color.white);
  }  
  
  public static void main(String[] args) {
    new ColorMenu();
  }
    
}

Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10328
Categories:Programming Code Examples, Java/J2EE/J2ME, AWT Components
Tags:Java/J2EE/J2MEAWT Components
Post Data:2017-01-02 16:04:35

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada