1 /* |
1 /* |
2 * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
3 * |
3 * |
4 * Redistribution and use in source and binary forms, with or without |
4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions |
5 * modification, are permitted provided that the following conditions |
6 * are met: |
6 * are met: |
7 * |
7 * |
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
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. |
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 */ |
30 */ |
31 |
31 |
32 /* |
|
33 */ |
|
34 |
32 |
35 import java.awt.*; |
|
36 import java.awt.event.*; |
|
37 import java.applet.Applet; |
33 import java.applet.Applet; |
|
34 import java.awt.BorderLayout; |
|
35 import java.awt.Button; |
|
36 import java.awt.CardLayout; |
|
37 import java.awt.Choice; |
|
38 import java.awt.Dimension; |
|
39 import java.awt.FlowLayout; |
|
40 import java.awt.Frame; |
|
41 import java.awt.GridLayout; |
|
42 import java.awt.LayoutManager; |
|
43 import java.awt.Panel; |
|
44 import java.awt.event.ActionEvent; |
|
45 import java.awt.event.ActionListener; |
|
46 import java.awt.event.ItemEvent; |
|
47 import java.awt.event.ItemListener; |
38 |
48 |
39 class CardPanel extends Panel { |
49 |
|
50 @SuppressWarnings("serial") |
|
51 final class CardPanel extends Panel { |
|
52 |
40 ActionListener listener; |
53 ActionListener listener; |
41 |
54 |
42 Panel create(LayoutManager layout) { |
55 Panel create(LayoutManager layout) { |
43 Button b = null; |
56 Button b = null; |
44 Panel p = new Panel(); |
57 Panel p = new Panel(); |
81 add("four", create(new BorderLayout(10, 10))); |
94 add("four", create(new BorderLayout(10, 10))); |
82 add("five", create(new FlowLayout(FlowLayout.LEFT, 10, 10))); |
95 add("five", create(new FlowLayout(FlowLayout.LEFT, 10, 10))); |
83 add("six", create(new GridLayout(2, 2, 10, 10))); |
96 add("six", create(new GridLayout(2, 2, 10, 10))); |
84 } |
97 } |
85 |
98 |
|
99 @Override |
86 public Dimension getPreferredSize() { |
100 public Dimension getPreferredSize() { |
87 return new Dimension(200, 100); |
101 return new Dimension(200, 100); |
88 } |
102 } |
89 } |
103 } |
90 |
104 |
|
105 |
|
106 @SuppressWarnings("serial") |
91 public class CardTest extends Applet |
107 public class CardTest extends Applet |
92 implements ActionListener, |
108 implements ActionListener, |
93 ItemListener { |
109 ItemListener { |
|
110 |
94 CardPanel cards; |
111 CardPanel cards; |
95 |
112 |
|
113 @SuppressWarnings("LeakingThisInConstructor") |
96 public CardTest() { |
114 public CardTest() { |
97 setLayout(new BorderLayout()); |
115 setLayout(new BorderLayout()); |
98 add("Center", cards = new CardPanel(this)); |
116 add("Center", cards = new CardPanel(this)); |
99 Panel p = new Panel(); |
117 Panel p = new Panel(); |
100 p.setLayout(new FlowLayout()); |
118 p.setLayout(new FlowLayout()); |
125 c.addItem("six"); |
143 c.addItem("six"); |
126 c.addItemListener(this); |
144 c.addItemListener(this); |
127 p.add(c); |
145 p.add(c); |
128 } |
146 } |
129 |
147 |
|
148 @Override |
130 public void itemStateChanged(ItemEvent e) { |
149 public void itemStateChanged(ItemEvent e) { |
131 ((CardLayout)cards.getLayout()).show(cards, |
150 ((CardLayout) cards.getLayout()).show(cards, |
132 (String)(e.getItem())); |
151 (String) (e.getItem())); |
133 } |
152 } |
134 |
153 |
|
154 @Override |
135 public void actionPerformed(ActionEvent e) { |
155 public void actionPerformed(ActionEvent e) { |
136 String arg = e.getActionCommand(); |
156 String arg = e.getActionCommand(); |
137 |
157 |
138 if ("first".equals(arg)) { |
158 if ("first".equals(arg)) { |
139 ((CardLayout)cards.getLayout()).first(cards); |
159 ((CardLayout) cards.getLayout()).first(cards); |
140 } else if ("next".equals(arg)) { |
160 } else if ("next".equals(arg)) { |
141 ((CardLayout)cards.getLayout()).next(cards); |
161 ((CardLayout) cards.getLayout()).next(cards); |
142 } else if ("previous".equals(arg)) { |
162 } else if ("previous".equals(arg)) { |
143 ((CardLayout)cards.getLayout()).previous(cards); |
163 ((CardLayout) cards.getLayout()).previous(cards); |
144 } else if ("last".equals(arg)) { |
164 } else if ("last".equals(arg)) { |
145 ((CardLayout)cards.getLayout()).last(cards); |
165 ((CardLayout) cards.getLayout()).last(cards); |
146 } else { |
166 } else { |
147 ((CardLayout)cards.getLayout()).show(cards,(String)arg); |
167 ((CardLayout) cards.getLayout()).show(cards, arg); |
148 } |
168 } |
149 } |
169 } |
150 |
170 |
151 public static void main(String args[]) { |
171 public static void main(String args[]) { |
152 Frame f = new Frame("CardTest"); |
172 Frame f = new Frame("CardTest"); |