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;
59 @SuppressWarnings(
"serial")
66 Panel p =
new Panel();
70 b =
new Button(
"one");
71 b.addActionListener(listener);
74 b =
new Button(
"two");
75 b.addActionListener(listener);
78 b =
new Button(
"three");
79 b.addActionListener(listener);
82 b =
new Button(
"four");
83 b.addActionListener(listener);
86 b =
new Button(
"five");
87 b.addActionListener(listener);
90 b =
new Button(
"six");
91 b.addActionListener(listener);
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)));
110 return new Dimension(200, 100);
115 @SuppressWarnings(
"serial")
117 implements ActionListener,
122 @SuppressWarnings(
"LeakingThisInConstructor")
124 setLayout(
new BorderLayout());
125 add(
"Center", cards =
new CardPanel(
this));
126 Panel p =
new Panel();
127 p.setLayout(
new FlowLayout());
130 Button b =
new Button(
"first");
131 b.addActionListener(
this);
134 b =
new Button(
"next");
135 b.addActionListener(
this);
138 b =
new Button(
"previous");
139 b.addActionListener(
this);
142 b =
new Button(
"last");
143 b.addActionListener(
this);
146 Choice c =
new Choice();
153 c.addItemListener(
this);
159 ((CardLayout) cards.getLayout()).show(cards,
160 (String) (e.getItem()));
165 String arg = e.getActionCommand();
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);
176 ((CardLayout) cards.getLayout()).show(cards, arg);
180 public static void main(String args[]) {
181 Frame f =
new Frame(
"CardTest");
186 f.add(
"Center", cardTest);
193 return "Demonstrates the different types of layout managers.";