home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 July & August / Pcwk78a98.iso / Internet / Javadraw / DATA.Z / CardTest.java < prev    next >
Text File  |  1997-04-02  |  4KB  |  153 lines

  1. /*
  2.  * @(#)CardTest.java    1.4 97/02/05
  3.  *
  4.  * Copyright (c) 1994-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. import java.awt.*;
  32. import java.awt.event.*;
  33. import java.applet.Applet;
  34.  
  35. class CardPanel extends Panel {
  36.     ActionListener listener;
  37.  
  38.     Panel create(LayoutManager layout) {
  39.     Button b = null;
  40.     Panel p = new Panel();
  41.  
  42.     p.setLayout(layout);
  43.  
  44.     b = new Button("one");
  45.     b.addActionListener(listener);
  46.     p.add("North", b);
  47.  
  48.     b = new Button("two");
  49.     b.addActionListener(listener);
  50.     p.add("West", b);
  51.  
  52.     b = new Button("three");
  53.     b.addActionListener(listener);
  54.     p.add("South", b);
  55.  
  56.     b = new Button("four");
  57.     b.addActionListener(listener);
  58.     p.add("East", b);
  59.  
  60.     b = new Button("five");
  61.     b.addActionListener(listener);
  62.     p.add("Center", b);
  63.  
  64.     return p;
  65.     }
  66.  
  67.     CardPanel(ActionListener actionListener) {
  68.     listener = actionListener;
  69.     setLayout(new CardLayout());
  70.     add("one", create(new FlowLayout()));
  71.     add("two", create(new BorderLayout()));
  72.     add("three", create(new GridLayout(2, 2)));
  73.     add("four", create(new BorderLayout(10, 10)));
  74.     add("five", create(new FlowLayout(FlowLayout.LEFT, 10, 10)));
  75.     add("six", create(new GridLayout(2, 2, 10, 10)));
  76.     }
  77.  
  78.     public Dimension getPreferredSize() {
  79.     return new Dimension(200, 100);
  80.     }
  81. }
  82.  
  83. public class CardTest extends Applet
  84.               implements ActionListener,
  85.                  ItemListener {
  86.     CardPanel cards;
  87.  
  88.     public CardTest() {
  89.     setLayout(new BorderLayout());
  90.     add("Center", cards = new CardPanel(this));
  91.     Panel p = new Panel();
  92.     p.setLayout(new FlowLayout());
  93.     add("South", p);
  94.  
  95.     Button b = new Button("first");
  96.     b.addActionListener(this);
  97.     p.add(b);
  98.  
  99.     b = new Button("next");
  100.     b.addActionListener(this);
  101.     p.add(b);
  102.  
  103.     b = new Button("previous");
  104.     b.addActionListener(this);
  105.     p.add(b);
  106.  
  107.     b = new Button("last");
  108.     b.addActionListener(this);
  109.     p.add(b);
  110.  
  111.     Choice c = new Choice();
  112.     c.addItem("one");
  113.     c.addItem("two");
  114.     c.addItem("three");
  115.     c.addItem("four");
  116.     c.addItem("five");
  117.     c.addItemListener(this);
  118.     p.add(c);
  119.     }
  120.  
  121.     public void itemStateChanged(ItemEvent e) {
  122.     ((CardLayout)cards.getLayout()).show(cards,
  123.                                          (String)(e.getItem()));
  124.     }
  125.  
  126.     public void actionPerformed(ActionEvent e) {
  127.     String arg = e.getActionCommand();
  128.  
  129.     if ("first".equals(arg)) {
  130.         ((CardLayout)cards.getLayout()).first(cards);
  131.     } else if ("next".equals(arg)) {
  132.         ((CardLayout)cards.getLayout()).next(cards);
  133.     } else if ("previous".equals(arg)) {
  134.         ((CardLayout)cards.getLayout()).previous(cards);
  135.     } else if ("last".equals(arg)) {
  136.         ((CardLayout)cards.getLayout()).last(cards);
  137.     } else {
  138.         ((CardLayout)cards.getLayout()).show(cards,(String)arg);
  139.     }
  140.     }
  141.  
  142.     public static void main(String args[]) {
  143.     Frame f = new Frame("CardTest");
  144.     CardTest cardTest = new CardTest();
  145.     cardTest.init();
  146.     cardTest.start();
  147.  
  148.     f.add("Center", cardTest);
  149.     f.setSize(300, 300);
  150.     f.show();
  151.     }
  152. }
  153.