home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / applets / CardTest / CardTest.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  4.7 KB  |  171 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)CardTest.java    1.9 02/06/13
  38.  */
  39.  
  40. import java.awt.*;
  41. import java.awt.event.*;
  42. import java.applet.Applet;
  43.  
  44. class CardPanel extends Panel {
  45.     ActionListener listener;
  46.  
  47.     Panel create(LayoutManager layout) {
  48.     Button b = null;
  49.     Panel p = new Panel();
  50.  
  51.     p.setLayout(layout);
  52.  
  53.     b = new Button("one");
  54.     b.addActionListener(listener);
  55.     p.add("North", b);
  56.  
  57.     b = new Button("two");
  58.     b.addActionListener(listener);
  59.     p.add("West", b);
  60.  
  61.     b = new Button("three");
  62.     b.addActionListener(listener);
  63.     p.add("South", b);
  64.  
  65.     b = new Button("four");
  66.     b.addActionListener(listener);
  67.     p.add("East", b);
  68.  
  69.     b = new Button("five");
  70.     b.addActionListener(listener);
  71.     p.add("Center", b);
  72.  
  73.     b = new Button("six");
  74.     b.addActionListener(listener);
  75.     p.add("Center", b);
  76.  
  77.     return p;
  78.     }
  79.  
  80.     CardPanel(ActionListener actionListener) {
  81.     listener = actionListener;
  82.     setLayout(new CardLayout());
  83.     add("one", create(new FlowLayout()));
  84.     add("two", create(new BorderLayout()));
  85.     add("three", create(new GridLayout(2, 2)));
  86.     add("four", create(new BorderLayout(10, 10)));
  87.     add("five", create(new FlowLayout(FlowLayout.LEFT, 10, 10)));
  88.     add("six", create(new GridLayout(2, 2, 10, 10)));
  89.     }
  90.  
  91.     public Dimension getPreferredSize() {
  92.     return new Dimension(200, 100);
  93.     }
  94. }
  95.  
  96. public class CardTest extends Applet
  97.               implements ActionListener,
  98.                  ItemListener {
  99.     CardPanel cards;
  100.  
  101.     public CardTest() {
  102.     setLayout(new BorderLayout());
  103.     add("Center", cards = new CardPanel(this));
  104.     Panel p = new Panel();
  105.     p.setLayout(new FlowLayout());
  106.     add("South", p);
  107.  
  108.     Button b = new Button("first");
  109.     b.addActionListener(this);
  110.     p.add(b);
  111.  
  112.     b = new Button("next");
  113.     b.addActionListener(this);
  114.     p.add(b);
  115.  
  116.     b = new Button("previous");
  117.     b.addActionListener(this);
  118.     p.add(b);
  119.  
  120.     b = new Button("last");
  121.     b.addActionListener(this);
  122.     p.add(b);
  123.  
  124.     Choice c = new Choice();
  125.     c.addItem("one");
  126.     c.addItem("two");
  127.     c.addItem("three");
  128.     c.addItem("four");
  129.     c.addItem("five");
  130.     c.addItem("six");
  131.     c.addItemListener(this);
  132.     p.add(c);
  133.     }
  134.  
  135.     public void itemStateChanged(ItemEvent e) {
  136.     ((CardLayout)cards.getLayout()).show(cards,
  137.                                          (String)(e.getItem()));
  138.     }
  139.  
  140.     public void actionPerformed(ActionEvent e) {
  141.     String arg = e.getActionCommand();
  142.  
  143.     if ("first".equals(arg)) {
  144.         ((CardLayout)cards.getLayout()).first(cards);
  145.     } else if ("next".equals(arg)) {
  146.         ((CardLayout)cards.getLayout()).next(cards);
  147.     } else if ("previous".equals(arg)) {
  148.         ((CardLayout)cards.getLayout()).previous(cards);
  149.     } else if ("last".equals(arg)) {
  150.         ((CardLayout)cards.getLayout()).last(cards);
  151.     } else {
  152.         ((CardLayout)cards.getLayout()).show(cards,(String)arg);
  153.     }
  154.     }
  155.  
  156.     public static void main(String args[]) {
  157.     Frame f = new Frame("CardTest");
  158.     CardTest cardTest = new CardTest();
  159.     cardTest.init();
  160.     cardTest.start();
  161.  
  162.     f.add("Center", cardTest);
  163.     f.setSize(300, 300);
  164.     f.show();
  165.     }
  166.     
  167.     public String getAppletInfo() {
  168.         return "Demonstrates the different types of layout managers.";
  169.     }
  170. }
  171.