home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / windows / doc / guiwindo.jav < prev    next >
Encoding:
Text File  |  1996-02-26  |  4.1 KB  |  141 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 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.  
  27.     MenuBar mb = new MenuBar();
  28.     Menu m = new Menu("Menu");
  29.     m.add(new MenuItem("Menu item 1"));
  30.     m.add(new CheckboxMenuItem("Menu item 2"));
  31.     m.add(new MenuItem("Menu item 3"));
  32.     m.add(new MenuItem("-"));
  33.     m.add(new MenuItem(FILEDIALOGMENUITEM));
  34.     mb.add(m);
  35.     setMenuBar(mb);
  36.  
  37.     //Add small things at the bottom.
  38.     bottomPanel.add(new TextField("TextField"));
  39.     bottomPanel.add(new Button("Button"));
  40.     bottomPanel.add(new Checkbox("Checkbox"));
  41.     Choice c = new Choice();
  42.     c.addItem("Choice Item 1");
  43.     c.addItem("Choice Item 2");
  44.     c.addItem("Choice Item 3");
  45.     bottomPanel.add(c);
  46.  
  47.     //Add big things to the center area.
  48.     centerPanel.setLayout(new GridLayout(1,2));
  49.     //Put a canvas in the left column.
  50.     centerPanel.add(new MyCanvas());
  51.     //Put a label and a text area in the right column.
  52.     Panel p = new Panel();
  53.     p.setLayout(new BorderLayout());
  54.     p.add("North", new Label("Label", Label.CENTER));
  55.     p.add("Center", new TextArea("TextArea", 5, 20));
  56.     centerPanel.add(p);
  57.  
  58.     setLayout(new BorderLayout());
  59.     add("South", bottomPanel);
  60.     add("Center", centerPanel);
  61.  
  62.     //Put a list in the window.
  63.     List l = new List(3, false);
  64.     l.addItem("List item 1");
  65.     l.addItem("List item 2");
  66.     l.addItem("List item 3");
  67.     l.addItem("List item 4");
  68.     l.addItem("List item 5");
  69.     l.addItem("List item 6");
  70.     l.addItem("List item 7");
  71.     l.addItem("List item 8");
  72.     l.addItem("List item 9");
  73.     l.addItem("List item 10");
  74.     add("East", l); 
  75.     }
  76.  
  77.     public boolean handleEvent(Event evt) {
  78.     if ((evt.id == Event.ACTION_EVENT) 
  79.         && (evt.target instanceof MenuItem)) {
  80.         String label = (String)evt.arg;
  81.         if (label.equals(FILEDIALOGMENUITEM)) {
  82.         FileDialog fd = new FileDialog(this, "FileDialog");
  83.         fd.show();
  84.         }
  85.         return true;
  86.     } 
  87.         if (evt.id == Event.WINDOW_ICONIFY) {//DOESN'T seem to be necessary
  88.             hide();
  89.             return true;
  90.         }
  91.         if (evt.id == Event.WINDOW_DESTROY) {
  92.             if (inAnApplet) {
  93.                 dispose();
  94.                 return true;
  95.             } else {
  96.                 System.exit(0);
  97.             }
  98.         }
  99.         return super.handleEvent(evt);
  100.     }
  101.  
  102.     public static void main(String args[]) {
  103.     GUIWindow window = new GUIWindow();
  104.         window.inAnApplet = false;
  105.  
  106.         //window.setTitle(window.getClass().getName() + " Application");
  107.         window.setTitle("The AWT Components");
  108.         window.pack();
  109.         window.show();
  110.     }
  111.  
  112. }
  113.  
  114. class MyCanvas extends Canvas {
  115.  
  116.     public void paint(Graphics g) {
  117.     int w = size().width;
  118.     int h = size().height;
  119.     g.drawRect(0, 0, w - 1, h - 1);
  120.     g.drawString("Canvas", (w - g.getFontMetrics().stringWidth("Canvas"))/2,
  121.               10);
  122.  
  123.     g.setFont(new Font("Helvetica", Font.PLAIN, 8));
  124.     g.drawLine(10,10, 100,100);
  125.     g.fillRect(9,9,3,3);
  126.     g.drawString("(10,10)", 13, 10);
  127.     g.fillRect(49,49,3,3);
  128.     g.drawString("(50,50)", 53, 50);
  129.     g.fillRect(99,99,3,3);
  130.     g.drawString("(100,100)", 103, 100);
  131.     }
  132.  
  133.     public Dimension minimumSize() {
  134.     return new Dimension(150,130);
  135.     }
  136.  
  137.     public Dimension preferredSize() {
  138.     return minimumSize();
  139.     }
  140. }
  141.