home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-06-13 | 2.5 KB | 89 lines |
- // TestFrame - implement our own frame
- import java.applet.*;
- import java.awt.*;
- class TestFrame extends Frame
- {
- // m_appletParent - this is the applet that
- // created the frame
- private Applet m_appletParent;
-
- // m_MenuBar - goes along the top of the frame
- private MenuBar m_MenuBar;
-
- // m_objSelected - this is the menu item selected
- private Object m_objSelected;
-
- TestFrame(Applet appletParent)
- {
- super("Test Frame");
-
- // save the parent applet
- m_appletParent = appletParent;
-
- // now create the menu bar
- m_MenuBar = new MenuBar();
-
- // add the File menu with its items
- Menu menuFile = new Menu("File");
- menuFile.add(new MenuItem("Open"));
- menuFile.add(new MenuItem("Close"));
- m_MenuBar.add(menuFile);
-
- // now the Edit menu with its items
- Menu menuEdit = new Menu("Edit");
- menuEdit.add(new MenuItem("Copy"));
- menuEdit.add(new MenuItem("Cut"));
- menuEdit.add(new MenuItem("Paste"));
- m_MenuBar.add(menuEdit);
-
- setMenuBar(m_MenuBar);
- }
-
- // action - called if any of the menu items are selected
- public boolean action(Event event, Object o)
- {
- // save the string of the menu item selected
- m_objSelected = o;
-
- // and cause the string to be painted
- repaint();
-
- // it is also possible to signal the parent
- if (m_appletParent != null)
- {
- m_appletParent.action(event, o);
- }
- return true;
- }
-
- public void paint(Graphics g)
- {
- String s;
- if (m_objSelected == null)
- {
- s = "No menu item selected";
- }
- else
- {
- s = "You selected " + m_objSelected.toString();
- }
-
- Dimension dimWin = size();
- Insets insFrame = insets();
- int nWinWidth =
- dimWin.width - (insFrame.left + insFrame.right);
- int nWinHeight =
- dimWin.height - (insFrame.top + insFrame.bottom);
-
- FontMetrics fm = getFontMetrics(getFont());
- int nStringHeight = fm.getHeight();
- int nStringWidth = fm.stringWidth(s);
-
- int nLeftOffset = (nWinWidth - nStringWidth) / 2;
- int nTopOffset = (nWinHeight + nStringHeight)/ 2;
-
- g.drawString(s, nLeftOffset, nTopOffset);
- g.drawRect(0, 0, nWinWidth - 1, nWinHeight - 1);
- }
- }
-