home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / windows / doc / ui / layout / example / gridbagwindow.java < prev    next >
Encoding:
Java Source  |  1996-02-26  |  3.1 KB  |  92 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 GridBagWindow extends Frame {
  20.     private boolean inAnApplet = true;
  21.  
  22.     protected void makebutton(String name,
  23.                               GridBagLayout gridbag,
  24.                               GridBagConstraints c) {
  25.         Button button = new Button(name);
  26.         gridbag.setConstraints(button, c);
  27.         add(button);
  28.     }
  29.  
  30.     public GridBagWindow() {
  31.         GridBagLayout gridbag = new GridBagLayout();
  32.         GridBagConstraints c = new GridBagConstraints();
  33.  
  34.         setFont(new Font("Helvetica", Font.PLAIN, 14));
  35.         setLayout(gridbag);
  36.    
  37.         c.fill = GridBagConstraints.BOTH;
  38.         c.weightx = 1.0;
  39.         makebutton("Button1", gridbag, c);
  40.         makebutton("Button2", gridbag, c);
  41.         makebutton("Button3", gridbag, c);
  42.  
  43.     c.gridwidth = GridBagConstraints.REMAINDER; //end row
  44.         makebutton("Button4", gridbag, c);
  45.  
  46.         c.weightx = 0.0;           //reset to the default
  47.         makebutton("Button5", gridbag, c); //another row
  48.  
  49.     c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
  50.         makebutton("Button6", gridbag, c);
  51.  
  52.     c.gridwidth = GridBagConstraints.REMAINDER; //end row
  53.         makebutton("Button7", gridbag, c);
  54.  
  55.     c.gridwidth = 1;              //reset to the default
  56.     c.gridheight = 2;
  57.         c.weighty = 1.0;
  58.         makebutton("Button8", gridbag, c);
  59.  
  60.         c.weighty = 0.0;           //reset to the default
  61.     c.gridwidth = GridBagConstraints.REMAINDER; //end row
  62.     c.gridheight = 1;           //reset to the default
  63.         makebutton("Button9", gridbag, c);
  64.         makebutton("Button10", gridbag, c);
  65.     }
  66.  
  67.     public synchronized boolean handleEvent(Event e) {
  68.         if (e.id == Event.WINDOW_ICONIFY) {//DOESN'T seem to be necessary
  69.             hide(); 
  70.             return true;
  71.         }
  72.         if (e.id == Event.WINDOW_DESTROY) {
  73.             if (inAnApplet) {
  74.                 dispose();
  75.                 return true;
  76.             } else {
  77.                 System.exit(0);
  78.             }
  79.         }   
  80.         return super.handleEvent(e);
  81.     }
  82.  
  83.     public static void main(String args[]) {
  84.     GridBagWindow window = new GridBagWindow();
  85.         window.inAnApplet = false;
  86.  
  87.     window.setTitle("GridBagWindow Application");
  88.     window.pack();
  89.     window.show();
  90.     }
  91. }
  92.