home *** CD-ROM | disk | FTP | other *** search
/ Freesoft 1997 May / Freesoft_1997-05_cd.bin / recenz / PROGRAM / JAVADRAW / iavadraw301_inst.exe / data.z / GUIWindow.java < prev    next >
Text File  |  1997-05-20  |  5KB  |  140 lines

  1. /*
  2.  * Copyright (c) 1995, 1996 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 GUIWindow extends Frame {
  20.     boolean inAnApplet = true;
  21.     final String FILEDIALOGMENUITEM = "File dialog...";
  22.  
  23.     public GUIWindow() {
  24.         Panel bottomPanel = new Panel();
  25.         Panel centerPanel = new Panel();
  26.         setLayout(new BorderLayout());
  27.  
  28.         //Set up the menu bar.
  29.         MenuBar mb = new MenuBar();
  30.         Menu m = new Menu("Menu");
  31.         m.add(new MenuItem("Menu item 1"));
  32.         m.add(new CheckboxMenuItem("Menu item 2"));
  33.         m.add(new MenuItem("Menu item 3"));
  34.         m.add(new MenuItem("-"));
  35.         m.add(new MenuItem(FILEDIALOGMENUITEM));
  36.         mb.add(m);
  37.         setMenuBar(mb);
  38.  
  39.         //Add small things at the bottom of the window.
  40.         bottomPanel.add(new TextField("TextField"));
  41.         bottomPanel.add(new Button("Button"));
  42.         bottomPanel.add(new Checkbox("Checkbox"));
  43.         Choice c = new Choice();
  44.         c.addItem("Choice Item 1");
  45.         c.addItem("Choice Item 2");
  46.         c.addItem("Choice Item 3");
  47.         bottomPanel.add(c);
  48.         add("South", bottomPanel);
  49.  
  50.         //Add big things to the center area of the window.
  51.         centerPanel.setLayout(new GridLayout(1,2));
  52.         //Put a canvas in the left column.
  53.         centerPanel.add(new MyCanvas());
  54.         //Put a label and a text area in the right column.
  55.         Panel p = new Panel();
  56.         p.setLayout(new BorderLayout());
  57.         p.add("North", new Label("Label", Label.CENTER));
  58.         p.add("Center", new TextArea("TextArea", 5, 20));
  59.         centerPanel.add(p);
  60.         add("Center", centerPanel);
  61.  
  62.         //Put a list on the right side of the window.
  63.         List l = new List(3, false);
  64.         for (int i = 1; i <= 10; i++) {
  65.             l.addItem("List item " + i);
  66.         }
  67.         add("East", l); 
  68.     }
  69.  
  70.     public boolean action(Event event, Object arg) {
  71.         //The only action event we pay attention to is when the
  72.         //user requests we bring up a FileDialog.
  73.         if (event.target instanceof MenuItem) {
  74.             if (((String)arg).equals(FILEDIALOGMENUITEM)) {
  75.                 FileDialog fd = new FileDialog(this, "FileDialog");
  76.                 fd.show();
  77.             }
  78.         }
  79.         return true;
  80.     }
  81.  
  82.     public boolean handleEvent(Event event) {
  83.         //If we're running as an application, closing the window
  84.         //should quit the application.
  85.         if (event.id == Event.WINDOW_DESTROY) {
  86.             if (inAnApplet) {
  87.                 dispose();
  88.             } else {
  89.                 System.exit(0);
  90.             }
  91.         }
  92.         return super.handleEvent(event);
  93.     }
  94.  
  95.     public static void main(String args[]) {
  96.         GUIWindow window = new GUIWindow();
  97.         window.inAnApplet = false;
  98.  
  99.         window.setTitle("The AWT Components");
  100.         window.pack();
  101.         window.show();
  102.     }
  103.  
  104. }
  105.  
  106. //We can't just instantiate Canvas, since its default implementation
  107. //gives us nothing interesting to look at or do.  So here's a Canvas
  108. //subclass that draws something slightly interesting.
  109. class MyCanvas extends Canvas {
  110.  
  111.     public void paint(Graphics g) {
  112.         int w = size().width;
  113.         int h = size().height;
  114.         g.drawRect(0, 0, w - 1, h - 1);
  115.         g.drawString("Canvas", (w - g.getFontMetrics().stringWidth("Canvas"))/2,
  116.                       10);
  117.  
  118.         g.setFont(new Font("Helvetica", Font.PLAIN, 8));
  119.         g.drawLine(10,10, 100,100);
  120.         g.fillRect(9,9,3,3);
  121.         g.drawString("(10,10)", 13, 10);
  122.         g.fillRect(49,49,3,3);
  123.         g.drawString("(50,50)", 53, 50);
  124.         g.fillRect(99,99,3,3);
  125.         g.drawString("(100,100)", 103, 100);
  126.     }
  127.  
  128.     //If we don't specify this, the canvas might not show up at all
  129.     //(depending on the layout manager).
  130.     public Dimension minimumSize() {
  131.         return new Dimension(150,130);
  132.     }
  133.  
  134.     //If we don't specify this, the canvas might not show up at all
  135.     //(depending on the layout manager).
  136.     public Dimension preferredSize() {
  137.         return minimumSize();
  138.     }
  139. }
  140.