Crossfire JXClient, Trunk
CardTest.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 1997, 2011, Oracle and/or its affiliates. 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  * - Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * - Neither the name of Oracle nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * This source code is provided to illustrate the usage of a given feature
34  * or technique and has been deliberately simplified. Additional steps
35  * required for a production-quality application, such as security checks,
36  * input validation and proper error handling, might not be present in
37  * this sample code.
38  */
39 
40 
41 
42 import java.applet.Applet;
43 import java.awt.BorderLayout;
44 import java.awt.Button;
45 import java.awt.CardLayout;
46 import java.awt.Choice;
47 import java.awt.Dimension;
48 import java.awt.FlowLayout;
49 import java.awt.Frame;
50 import java.awt.GridLayout;
51 import java.awt.LayoutManager;
52 import java.awt.Panel;
53 import java.awt.event.ActionEvent;
54 import java.awt.event.ActionListener;
55 import java.awt.event.ItemEvent;
56 import java.awt.event.ItemListener;
57 
58 
59 @SuppressWarnings("serial")
60 final class CardPanel extends Panel {
61 
62  ActionListener listener;
63 
64  Panel create(LayoutManager layout) {
65  Button b = null;
66  Panel p = new Panel();
67 
68  p.setLayout(layout);
69 
70  b = new Button("one");
71  b.addActionListener(listener);
72  p.add("North", b);
73 
74  b = new Button("two");
75  b.addActionListener(listener);
76  p.add("West", b);
77 
78  b = new Button("three");
79  b.addActionListener(listener);
80  p.add("South", b);
81 
82  b = new Button("four");
83  b.addActionListener(listener);
84  p.add("East", b);
85 
86  b = new Button("five");
87  b.addActionListener(listener);
88  p.add("Center", b);
89 
90  b = new Button("six");
91  b.addActionListener(listener);
92  p.add("Center", b);
93 
94  return p;
95  }
96 
97  CardPanel(ActionListener actionListener) {
98  listener = actionListener;
99  setLayout(new CardLayout());
100  add("one", create(new FlowLayout()));
101  add("two", create(new BorderLayout()));
102  add("three", create(new GridLayout(2, 2)));
103  add("four", create(new BorderLayout(10, 10)));
104  add("five", create(new FlowLayout(FlowLayout.LEFT, 10, 10)));
105  add("six", create(new GridLayout(2, 2, 10, 10)));
106  }
107 
108  @Override
109  public Dimension getPreferredSize() {
110  return new Dimension(200, 100);
111  }
112 }
113 
114 
115 @SuppressWarnings("serial")
116 public class CardTest extends Applet
117  implements ActionListener,
118  ItemListener {
119 
120  CardPanel cards;
121 
122  @SuppressWarnings("LeakingThisInConstructor")
123  public CardTest() {
124  setLayout(new BorderLayout());
125  add("Center", cards = new CardPanel(this));
126  Panel p = new Panel();
127  p.setLayout(new FlowLayout());
128  add("South", p);
129 
130  Button b = new Button("first");
131  b.addActionListener(this);
132  p.add(b);
133 
134  b = new Button("next");
135  b.addActionListener(this);
136  p.add(b);
137 
138  b = new Button("previous");
139  b.addActionListener(this);
140  p.add(b);
141 
142  b = new Button("last");
143  b.addActionListener(this);
144  p.add(b);
145 
146  Choice c = new Choice();
147  c.addItem("one");
148  c.addItem("two");
149  c.addItem("three");
150  c.addItem("four");
151  c.addItem("five");
152  c.addItem("six");
153  c.addItemListener(this);
154  p.add(c);
155  }
156 
157  @Override
158  public void itemStateChanged(ItemEvent e) {
159  ((CardLayout) cards.getLayout()).show(cards,
160  (String) (e.getItem()));
161  }
162 
163  @Override
164  public void actionPerformed(ActionEvent e) {
165  String arg = e.getActionCommand();
166 
167  if ("first".equals(arg)) {
168  ((CardLayout) cards.getLayout()).first(cards);
169  } else if ("next".equals(arg)) {
170  ((CardLayout) cards.getLayout()).next(cards);
171  } else if ("previous".equals(arg)) {
172  ((CardLayout) cards.getLayout()).previous(cards);
173  } else if ("last".equals(arg)) {
174  ((CardLayout) cards.getLayout()).last(cards);
175  } else {
176  ((CardLayout) cards.getLayout()).show(cards, arg);
177  }
178  }
179 
180  public static void main(String args[]) {
181  Frame f = new Frame("CardTest");
182  CardTest cardTest = new CardTest();
183  cardTest.init();
184  cardTest.start();
185 
186  f.add("Center", cardTest);
187  f.setSize(300, 300);
188  f.setVisible(true);
189  }
190 
191  @Override
192  public String getAppletInfo() {
193  return "Demonstrates the different types of layout managers.";
194  }
195 }
CardTest.itemStateChanged
void itemStateChanged(ItemEvent e)
Definition: CardTest.java:158
CardTest.getAppletInfo
String getAppletInfo()
Definition: CardTest.java:192
CardTest.main
static void main(String args[])
Definition: CardTest.java:180
CardTest.actionPerformed
void actionPerformed(ActionEvent e)
Definition: CardTest.java:164
CardTest
Definition: CardTest.java:116