home *** CD-ROM | disk | FTP | other *** search
/ Learn Java Now / Learn_Java_Now_Microsoft_1996.iso / JavaNow / Code / AppB / CardTest / CardTest.java < prev    next >
Encoding:
Java Source  |  1996-07-09  |  4.6 KB  |  132 lines

  1. /*
  2.  * @(#)CardTest.java    1.7 95/08/23 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights
  5.  * Reserved.
  6.  *
  7.  * Permission to use, copy, modify, and distribute this software
  8.  * and its documentation for NON-COMMERCIAL or COMMERCIAL 
  9.  * purposes and without fee is hereby granted. 
  10.  * Please refer to the file 
  11.  * http://java.sun.com/copy_trademarks.html for further important 
  12.  * copyright and trademark information and to
  13.  * http://java.sun.com/licensing.html for further important 
  14.  * licensing information for the Java (tm) Technology.
  15.  * 
  16.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE 
  17.  * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, 
  18.  * INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF 
  19.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-
  20.  * INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED 
  21.  * BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  22.  * THIS SOFTWARE OR ITS DERIVATIVES.
  23.  * 
  24.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS 
  25.  * ON-LINE CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING 
  26.  * FAIL-SAFE PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR 
  27.  * FACILITIES, AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR 
  28.  * TRAFFIC CONTROL, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS 
  29.  * SYSTEMS, IN WHICH THE FAILURE OF THE SOFTWARE COULD LEAD 
  30.  * DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL OR 
  31.  * ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN 
  32.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF 
  33.  * FITNESS FOR HIGH RISK ACTIVITIES.
  34.  */
  35.  
  36. import java.awt.*;
  37. import java.applet.Applet;
  38.  
  39. // CardPanel - a CardPanel is a Panel with the 5 buttons
  40. //             labelled one through five and with a specified
  41. //             layout manager.
  42. class CardPanel extends Panel {
  43.     Panel create(LayoutManager layout) {
  44.     Panel p = new Panel();
  45.     p.setLayout(layout);
  46.     p.add("North",   new Button("one"));
  47.     p.add("West",   new Button("two"));
  48.     p.add("South",  new Button("three"));
  49.     p.add("East",   new Button("four"));
  50.     p.add("Center", new Button("five"));
  51.     return p;
  52.     }
  53.  
  54.     CardPanel() {
  55.     setLayout(new CardLayout());
  56.     add("one", create(new FlowLayout()));
  57.     add("two", create(new BorderLayout()));
  58.     add("three", create(new GridLayout(2, 2)));
  59.     add("four", create(new BorderLayout(10, 10)));
  60.     add("five", create(new FlowLayout(FlowLayout.LEFT, 10, 10)));
  61.     add("six", create(new GridLayout(2, 2, 10, 10)));
  62.     }
  63.  
  64.     public Dimension preferredSize() {
  65.     return new Dimension(200, 100);
  66.     }
  67. }
  68.  
  69. public class CardTest extends Applet {
  70.     CardPanel cards;
  71.  
  72.     public CardTest() {
  73.     setLayout(new BorderLayout());
  74.     add("Center", cards = new CardPanel());
  75.     Panel p = new Panel();
  76.     p.setLayout(new FlowLayout());
  77.     add("South", p);
  78.     p.add(new Button("first"));
  79.     p.add(new Button("next"));
  80.     p.add(new Button("previous"));
  81.     p.add(new Button("last"));
  82.     Choice c = new Choice();
  83.     c.addItem("one");
  84.     c.addItem("two");
  85.     c.addItem("three");
  86.     c.addItem("four");
  87.     c.addItem("five");
  88.     p.add(c);
  89.     }
  90.  
  91.     // the action is based on the label of the button;
  92.     // this allows several buttons that share a common label,
  93.     // ("one" for example), to have the same action
  94.     public boolean action(Event evt, Object arg) {
  95.     if (evt.target instanceof Choice) {
  96.         ((CardLayout)cards.getLayout()).show(cards,(String)arg);
  97.     } else {
  98.         if ("first".equals(arg)) {
  99.         ((CardLayout)cards.getLayout()).first(cards);
  100.         } else if ("next".equals(arg)) {
  101.         ((CardLayout)cards.getLayout()).next(cards);
  102.         } else if ("previous".equals(arg)) {
  103.         ((CardLayout)cards.getLayout()).previous(cards);
  104.         } else if ("last".equals(arg)) {
  105.         ((CardLayout)cards.getLayout()).last(cards);
  106.         } else {
  107.         ((CardLayout)cards.getLayout()).show(cards,(String)arg);
  108.         }
  109.     }
  110.     return true;
  111.     }
  112.  
  113.     // main is executed only when the applet is executed as
  114.     // an application. main goes through the same steps a
  115.     // browser would go through when displaying the applet's
  116.     // window:
  117.     // a) create a window in which to display the output
  118.     // b) call the constructor for the applet class
  119.     // c) call the applet init function
  120.     // d) call the applet start function
  121.     public static void main(String args[]) {
  122.     Frame f = new Frame("CardTest");
  123.     CardTest cardTest = new CardTest();
  124.     cardTest.init();
  125.     cardTest.start();
  126.  
  127.     f.add("Center", cardTest);
  128.     f.resize(300, 300);
  129.     f.show();
  130.     }
  131. }
  132.