home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / ui / layout / example / NoneWindow.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  2.7 KB  |  81 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18.  
  19. public class NoneWindow extends Frame {
  20.     private boolean inAnApplet = true;
  21.     private boolean laidOut = false;
  22.     private Button b1, b2, b3;
  23.  
  24.     public NoneWindow() {
  25.         super();
  26.         setLayout(null);
  27.         setFont(new Font("Helvetica", Font.PLAIN, 14));
  28.  
  29.         b1 = new Button("one");
  30.         add(b1);
  31.         b2 = new Button("two");
  32.         add(b2);
  33.         b3 = new Button("three");
  34.         add(b3);
  35.     }
  36.  
  37.     public void paint(Graphics g) {
  38.         if (!laidOut) {
  39.             Insets insets = insets();
  40.             /* 
  41.              * We're guaranteed that insets() will return a valid Insets
  42.              * if called from paint() -- it isn't valid when called from 
  43.              * the constructor.
  44.              *
  45.              * We could perhaps cache this in an ivar, but insets can
  46.              * change, and when they do, the AWT creates a whole new
  47.              * Insets object; the old one is invalid.
  48.              */
  49.             b1.reshape(50 + insets.left, 5 + insets.top, 50, 20);
  50.             b2.reshape(70 + insets.left, 35 + insets.top, 50, 20);
  51.             b3.reshape(130 + insets.left, 15 + insets.top, 50, 30);
  52.  
  53.             laidOut = true;
  54.         }
  55.     }
  56.  
  57.     public boolean handleEvent(Event e) {
  58.         if (e.id == Event.WINDOW_DESTROY) {
  59.             if (inAnApplet) {
  60.                 dispose();
  61.                 return false;
  62.             } else {
  63.                 System.exit(0);
  64.             }
  65.         }   
  66.         return super.handleEvent(e);
  67.     }
  68.  
  69.     public static void main(String args[]) {
  70.         NoneWindow window = new NoneWindow();
  71.         Insets insets = window.insets();
  72.         //How do we know insets is valid here?
  73.         window.inAnApplet = false;
  74.  
  75.         window.setTitle("NoneWindow Application");
  76.         window.resize(250 + insets.left + insets.right,
  77.                       90 + insets.top + insets.bottom);
  78.         window.show();
  79.     }
  80. }
  81.