home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / Java++ / VJ / SAMPLES / JAVANOW / CHAP17 / FRAMECOMPONENT / TESTFRAME.JAVA < prev   
Encoding:
Java Source  |  1996-06-13  |  2.5 KB  |  89 lines

  1. // TestFrame - implement our own frame
  2. import java.applet.*;
  3. import java.awt.*;
  4. class TestFrame extends Frame
  5. {
  6.     // m_appletParent - this is the applet that
  7.     //                  created the frame
  8.     private Applet  m_appletParent;
  9.  
  10.     // m_MenuBar - goes along the top of the frame
  11.     private MenuBar m_MenuBar;
  12.  
  13.     // m_objSelected - this is the menu item selected
  14.     private Object m_objSelected;
  15.  
  16.     TestFrame(Applet appletParent)
  17.     {
  18.         super("Test Frame");
  19.  
  20.         // save the parent applet
  21.         m_appletParent = appletParent;
  22.  
  23.         // now create the menu bar
  24.         m_MenuBar = new MenuBar();
  25.  
  26.         // add the File menu with its items
  27.         Menu menuFile = new Menu("File");
  28.         menuFile.add(new MenuItem("Open"));
  29.         menuFile.add(new MenuItem("Close"));
  30.         m_MenuBar.add(menuFile);
  31.  
  32.         // now the Edit menu with its items
  33.         Menu menuEdit = new Menu("Edit");
  34.         menuEdit.add(new MenuItem("Copy"));
  35.         menuEdit.add(new MenuItem("Cut"));
  36.         menuEdit.add(new MenuItem("Paste"));
  37.         m_MenuBar.add(menuEdit);
  38.  
  39.         setMenuBar(m_MenuBar);
  40.     }
  41.  
  42.     // action - called if any of the menu items are selected
  43.     public boolean action(Event event, Object o)
  44.     {
  45.         // save the string of the menu item selected
  46.         m_objSelected = o;
  47.  
  48.         // and cause the string to be painted
  49.         repaint();
  50.  
  51.         // it is also possible to signal the parent
  52.         if (m_appletParent != null)
  53.         {
  54.             m_appletParent.action(event, o);
  55.         }
  56.         return true;
  57.     }
  58.  
  59.     public void paint(Graphics g)
  60.     {
  61.         String s;
  62.         if (m_objSelected == null)
  63.         {
  64.             s = "No menu item selected";
  65.         }
  66.         else
  67.         {
  68.             s = "You selected " + m_objSelected.toString();
  69.         }
  70.  
  71.         Dimension dimWin = size();
  72.         Insets insFrame = insets();
  73.         int nWinWidth  = 
  74.                dimWin.width  - (insFrame.left + insFrame.right);
  75.         int nWinHeight = 
  76.                dimWin.height - (insFrame.top  + insFrame.bottom);
  77.  
  78.         FontMetrics fm = getFontMetrics(getFont());
  79.         int nStringHeight = fm.getHeight();
  80.         int nStringWidth  = fm.stringWidth(s);
  81.  
  82.         int nLeftOffset = (nWinWidth  - nStringWidth) / 2;
  83.         int nTopOffset  = (nWinHeight + nStringHeight)/ 2;
  84.  
  85.         g.drawString(s, nLeftOffset, nTopOffset);
  86.         g.drawRect(0, 0, nWinWidth - 1, nWinHeight - 1);
  87.     } 
  88. }
  89.