home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / windows / doc / ui / layout / example / nonewindow.java < prev    next >
Encoding:
Java Source  |  1996-02-26  |  2.7 KB  |  83 lines

  1. /*
  2.  * Copyright (c) 1994 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.         //We're guaranteed that insets() will return a valid Insets
  41.         //if called from paint() -- it isn't valid when called from 
  42.         //the constructor.
  43.         insets = insets(); // We could perhaps cache this in an ivar, but
  44.                   // insets can change, and when they
  45.                   // do, the AWT creates a whole new Insets
  46.                   // object; the old one is invalid.
  47.                   // BUT WILL IT REALLY CHANGE???
  48.         b1.reshape(50 + insets.left, 5 + insets.top, 50, 20);
  49.         b2.reshape(70 + insets.left, 35 + insets.top, 50, 20);
  50.         b3.reshape(130 + insets.left, 15 + insets.top, 50, 30);
  51.  
  52.         laidOut = true;
  53.     }
  54.     }
  55.  
  56.     public synchronized boolean handleEvent(Event e) {
  57.         if (e.id == Event.WINDOW_ICONIFY) {//DOESN'T seem to be necessary
  58.             hide(); 
  59.         }
  60.         if (e.id == Event.WINDOW_DESTROY) {
  61.             if (inAnApplet) {
  62.                 dispose();
  63.                 return false;
  64.             } else {
  65.                 System.exit(0);
  66.             }
  67.         }   
  68.         return super.handleEvent(e);
  69.     }
  70.  
  71.     public static void main(String args[]) {
  72.     NoneWindow window = new NoneWindow();
  73.     Insets insets = window.insets();
  74.     //How do we know insets is valid here?
  75.         window.inAnApplet = false;
  76.  
  77.     window.setTitle("NoneWindow Application");
  78.     window.resize(250 + insets.left + insets.right,
  79.               90 + insets.top + insets.bottom);
  80.         window.show();
  81.     }
  82. }
  83.