home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1997 May / PC Plus Super CD Issue 127 (May 1997).iso / handson / java / classes / frame1.java < prev    next >
Encoding:
Java Source  |  1997-02-07  |  5.2 KB  |  216 lines

  1. /*
  2.     PC Plus sample Java app.
  3.     Demonstrates the definition of user-defined Classes.
  4. */
  5.  
  6. import java.awt.*;
  7. import java.util.*;
  8.  
  9. public class Frame1 extends Frame {
  10.  
  11.     Vector v = new Vector();
  12.  
  13.     void button1_Clicked(Event event) {
  14.         // v is a Vector of mixed Objects.
  15.         // This method checks the Object types using 'instanceof'
  16.         // Then it prints the results into a List box.
  17.  
  18.         list1.clear();
  19.         for (Enumeration e = v.elements(); e.hasMoreElements(); ) {
  20.             Thing t = (Thing)e.nextElement();
  21.             list1.addItem(t.getName());
  22.             list1.addItem(t.getDescription());
  23.             if (t instanceof Object)
  24.                 list1.addItem("[This is an Object]");
  25.             if (t instanceof Thing)
  26.                 list1.addItem("[This is a Thing]");
  27.             if (t instanceof Creature)
  28.                 list1.addItem("[This is a Creature] species = " + ((Creature) t).getSpecies());
  29.             if (t instanceof Treasure)
  30.                 list1.addItem("[This is a Treasure] value = " + ((Treasure) t).getValue());
  31.             list1.addItem( "--------------------" );
  32.  
  33.         }
  34.  
  35.  
  36.     }
  37.  
  38.     void Open_Action(Event event) {
  39.         OpenFileDialog.show();
  40.     }
  41.  
  42.     void About_Action(Event event) {
  43.         (new AboutDialog(this, "About...", false)).show();
  44.     }
  45.  
  46.     void Exit_Action(Event event) {
  47.         (new QuitDialog(this, "Quit the Application?", false)).show();
  48.     }
  49.  
  50.     public Frame1() {
  51.  
  52.         //{{INIT_CONTROLS
  53.         setLayout(null);
  54.         addNotify();
  55.         resize(insets().left + insets().right + 431,insets().top + insets().bottom + 464);
  56.         OpenFileDialog = new java.awt.FileDialog(this, "Open",FileDialog.LOAD);
  57.         list1 = new java.awt.List(0,false);
  58.         add(list1);
  59.         list1.reshape(insets().left + 7,insets().top + 8,416,345);
  60.         button1 = new java.awt.Button("Display My Objects!");
  61.         button1.reshape(insets().left + 133,insets().top + 383,159,39);
  62.         add(button1);
  63.         setTitle("A Basic Application");
  64.         //}}
  65.  
  66.         //{{INIT_MENUS
  67.         mainMenuBar = new java.awt.MenuBar();
  68.  
  69.         menu1 = new java.awt.Menu("File");
  70.         menu1.add("Open...");
  71.         menu1.add("Save");
  72.         menu1.add("Save As...");
  73.         menu1.addSeparator();
  74.         menu1.add("Exit");
  75.         mainMenuBar.add(menu1);
  76.  
  77.         menu2 = new java.awt.Menu("Edit");
  78.         menu2.add("Cut");
  79.         menu2.add("Copy");
  80.         menu2.add("Paste");
  81.         mainMenuBar.add(menu2);
  82.  
  83.         menu3 = new java.awt.Menu("Help");
  84.         menu3.add("About");
  85.         mainMenuBar.add(menu3);
  86.         setMenuBar(mainMenuBar);
  87.         //}}
  88.     }
  89.  
  90.     public Frame1(String title) {
  91.         this();
  92.         setTitle(title);
  93.     }
  94.  
  95.     public synchronized void show() {
  96.         move(50, 50);
  97.         super.show();
  98.  
  99.     v.addElement( new Thing("A Wotsit", "An ordinary Thing"));
  100.     v.addElement( new Creature("A Troll", "A nasty fellow", "Trollus trollidus" ));
  101.     v.addElement( new Treasure("A Sword", "A jewel-encrusted weapon", 550));
  102.  
  103.     }
  104.  
  105.     public boolean handleEvent(Event event) {
  106.         if (event.id == Event.WINDOW_DESTROY) {
  107.             hide();         // hide the Frame
  108.             dispose();
  109.             System.exit(0);
  110.             return true;
  111.         }
  112.         if (event.target == button1 && event.id == Event.ACTION_EVENT) {
  113.             button1_Clicked(event);
  114.         }
  115.         return super.handleEvent(event);
  116.     }
  117.  
  118.     public boolean action(Event event, Object arg) {
  119.         if (event.target instanceof MenuItem) {
  120.             String label = (String) arg;
  121.             if (label.equalsIgnoreCase("Open...")) {
  122.                 Open_Action(event);
  123.                 return true;
  124.             } else
  125.             if (label.equalsIgnoreCase("About")) {
  126.                 About_Action(event);
  127.                 return true;
  128.             } else
  129.             if (label.equalsIgnoreCase("Exit")) {
  130.                 Exit_Action(event);
  131.                 return true;
  132.             }
  133.         }
  134.         return super.action(event, arg);
  135.     }
  136.  
  137.     static public void main(String args[]) {
  138.         (new Frame1()).show();
  139.     }
  140.  
  141.     //{{DECLARE_CONTROLS
  142.     java.awt.FileDialog OpenFileDialog;
  143.     java.awt.List list1;
  144.     java.awt.Button button1;
  145.     //}}
  146.  
  147.     //{{DECLARE_MENUS
  148.     java.awt.MenuBar mainMenuBar;
  149.     java.awt.Menu menu1;
  150.     java.awt.Menu menu2;
  151.     java.awt.Menu menu3;
  152.     //}}
  153. }
  154.  
  155.  
  156. // ---- OUR OWN USER-DEFINED CLASS HIERARCHY ----
  157. // class Thing
  158. // any object that has a name and a description
  159. class Thing {
  160.     private String name;
  161.     private String description;
  162.  
  163.     Thing( String aName, String aDescription) {
  164.         this.name = aName;
  165.         this.description = aDescription;
  166.     }
  167.  
  168.     String getName() {
  169.         return name;
  170.     }
  171.  
  172.     void setName(String aName) {
  173.         this.name = aName;
  174.     }
  175.  
  176.     String getDescription() {
  177.         return description;
  178.     }
  179.  
  180.     void setDescription(String aDescription) {
  181.         this.description = aDescription;
  182.     }
  183. } // END: class Thing
  184.  
  185.  
  186. // class Creature
  187. // a descendent of Thing which also has a 'species'
  188. class Creature extends Thing {
  189.     private String species;
  190.  
  191.     Creature( String aName, String aDescription, String aSpecies ) {
  192.         super( aName, aDescription );
  193.         this.species = aSpecies;
  194.     }
  195.  
  196.    String getSpecies() {
  197.         return species;
  198.    }
  199.  
  200. }
  201.  
  202. // class Treasure
  203. // a descendent of Thing which also has a 'value'
  204. class Treasure extends Thing {
  205.     private int value;
  206.  
  207.     Treasure( String aName, String aDescription, int aValue ) {
  208.         super( aName, aDescription );
  209.         this.value = aValue;
  210.     }
  211.  
  212.    int getValue() {
  213.         return value;
  214.    }
  215.  
  216. }