KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > CardTest


1 /*
2  * @(#)CardTest.java 1.14 06/02/22
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)CardTest.java 1.14 06/02/22
39  */

40
41 import java.awt.*;
42 import java.awt.event.*;
43 import java.applet.Applet JavaDoc;
44
45 class CardPanel extends Panel {
46     ActionListener listener;
47
48     Panel create(LayoutManager layout) {
49     Button b = null;
50     Panel p = new Panel();
51
52     p.setLayout(layout);
53
54     b = new Button("one");
55     b.addActionListener(listener);
56     p.add("North", b);
57
58     b = new Button("two");
59     b.addActionListener(listener);
60     p.add("West", b);
61
62     b = new Button("three");
63     b.addActionListener(listener);
64     p.add("South", b);
65
66     b = new Button("four");
67     b.addActionListener(listener);
68     p.add("East", b);
69
70     b = new Button("five");
71     b.addActionListener(listener);
72     p.add("Center", b);
73
74     b = new Button("six");
75     b.addActionListener(listener);
76     p.add("Center", b);
77
78     return p;
79     }
80
81     CardPanel(ActionListener actionListener) {
82     listener = actionListener;
83     setLayout(new CardLayout());
84     add("one", create(new FlowLayout()));
85     add("two", create(new BorderLayout()));
86     add("three", create(new GridLayout(2, 2)));
87     add("four", create(new BorderLayout(10, 10)));
88     add("five", create(new FlowLayout(FlowLayout.LEFT, 10, 10)));
89     add("six", create(new GridLayout(2, 2, 10, 10)));
90     }
91
92     public Dimension getPreferredSize() {
93     return new Dimension(200, 100);
94     }
95 }
96
97 public class CardTest extends Applet JavaDoc
98               implements ActionListener,
99                  ItemListener {
100     CardPanel cards;
101
102     public CardTest() {
103     setLayout(new BorderLayout());
104     add("Center", cards = new CardPanel(this));
105     Panel p = new Panel();
106     p.setLayout(new FlowLayout());
107     add("South", p);
108
109     Button b = new Button("first");
110     b.addActionListener(this);
111     p.add(b);
112
113     b = new Button("next");
114     b.addActionListener(this);
115     p.add(b);
116
117     b = new Button("previous");
118     b.addActionListener(this);
119     p.add(b);
120
121     b = new Button("last");
122     b.addActionListener(this);
123     p.add(b);
124
125     Choice c = new Choice();
126     c.addItem("one");
127     c.addItem("two");
128     c.addItem("three");
129     c.addItem("four");
130     c.addItem("five");
131     c.addItem("six");
132     c.addItemListener(this);
133     p.add(c);
134     }
135
136     public void itemStateChanged(ItemEvent e) {
137     ((CardLayout)cards.getLayout()).show(cards,
138                                          (String JavaDoc)(e.getItem()));
139     }
140
141     public void actionPerformed(ActionEvent e) {
142     String JavaDoc arg = e.getActionCommand();
143
144     if ("first".equals(arg)) {
145         ((CardLayout)cards.getLayout()).first(cards);
146     } else if ("next".equals(arg)) {
147         ((CardLayout)cards.getLayout()).next(cards);
148     } else if ("previous".equals(arg)) {
149         ((CardLayout)cards.getLayout()).previous(cards);
150     } else if ("last".equals(arg)) {
151         ((CardLayout)cards.getLayout()).last(cards);
152     } else {
153         ((CardLayout)cards.getLayout()).show(cards,(String JavaDoc)arg);
154     }
155     }
156
157     public static void main(String JavaDoc args[]) {
158     Frame f = new Frame("CardTest");
159     CardTest cardTest = new CardTest();
160     cardTest.init();
161     cardTest.start();
162
163     f.add("Center", cardTest);
164     f.setSize(300, 300);
165     f.show();
166     }
167     
168     public String JavaDoc getAppletInfo() {
169         return "Demonstrates the different types of layout managers.";
170     }
171 }
172
Popular Tags