1 36 37 40 41 import java.awt.*; 42 import java.awt.event.*; 43 import java.applet.Applet ; 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  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 )(e.getItem())); 139 } 140 141 public void actionPerformed(ActionEvent e) { 142 String 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 )arg); 154 } 155 } 156 157 public static void main(String 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 getAppletInfo() { 169 return "Demonstrates the different types of layout managers."; 170 } 171 } 172
| Popular Tags
|