home *** CD-ROM | disk | FTP | other *** search
/ Java by Example / jbecd.bin / JBE-CD / NTUsers / JBECODE.ZIP / JavaByExample / chap23 / MenuBarApplet.java < prev    next >
Text File  |  1996-03-06  |  2KB  |  90 lines

  1. import java.awt.*;
  2. import java.applet.*;
  3.  
  4. public class MenuBarApplet extends Applet
  5. {
  6.     MenuBarFrame frame;
  7.     Button button;
  8.  
  9.     public void init()
  10.     {
  11.           frame = new MenuBarFrame("MenuBar Window");
  12.  
  13.           button = new Button("Show Window");
  14.           add(button);
  15.     }
  16.  
  17.     public boolean action(Event evt, Object arg)
  18.     {
  19.         boolean visible = frame.isShowing();
  20.         if (visible)
  21.         {
  22.             frame.hide();
  23.             button.setLabel("Show Window");
  24.         }
  25.         else
  26.         {
  27.             frame.show();
  28.             button.setLabel("Hide Window");
  29.         }
  30.  
  31.         return true;
  32.     }
  33. }
  34.  
  35. class MenuBarFrame extends Frame
  36. {
  37.     MenuBar menuBar;
  38.     String str;
  39.  
  40.     MenuBarFrame(String title)
  41.     {
  42.         super(title);
  43.         menuBar = new MenuBar();
  44.         setMenuBar(menuBar);
  45.  
  46.         Menu menu = new Menu("Test");
  47.         menuBar.add(menu);
  48.  
  49.         MenuItem item = new MenuItem("Command 1");
  50.         menu.add(item);
  51.         item = new MenuItem("Command 2");
  52.         menu.add(item);
  53.  
  54.         item = new MenuItem("-");
  55.         menu.add(item);
  56.  
  57.         CheckboxMenuItem checkItem =
  58.             new CheckboxMenuItem("Check");
  59.         menu.add(checkItem);
  60.  
  61.         str = "";
  62.         Font font = new Font("TimesRoman", Font.BOLD, 20);
  63.         setFont(font);
  64.     }
  65.  
  66.     public void paint(Graphics g)
  67.     {
  68.         resize(300, 250);
  69.         g.drawString(str, 20, 100);
  70.     }
  71.  
  72.     public boolean action(Event evt, Object arg)
  73.     {
  74.         if (evt.target instanceof MenuItem)
  75.         {
  76.             if (arg == "Command 1")
  77.                 str = "You selected Command 1";
  78.             else if (arg == "Command 2")
  79.                 str = "You selected Command 2";
  80.             else if (arg == "Check")
  81.                 str = "You selected the Check item";
  82.  
  83.             repaint();
  84.             return true;
  85.         }
  86.         else
  87.             return false;
  88.     }
  89. }
  90.