cardlayoutdemo.txt
来自「JAVA的一些基础教程」· 文本 代码 · 共 69 行
TXT
69 行
1: import java.awt.*;
2: import java.awt.event.*;
3:
4: public class CardLayoutDemo implements ActionListener {
5: Panel p1, p2, p3, p4;
6: int i = 1;
7: Frame f;
8:
9: public CardLayoutDemo() {
10: f = new Frame("cardLayoutDemo");
11: f.setLayout(new GridLayout(2, 1));
12: p1 = new Panel();
13: Button b = new Button("change card");
14: b.addActionListener(this);
15: p1.add(b);
16: f.add(p1);
17:
18: p2 = new Panel();
19: //p2中设置组件
20: p2.setLayout(new FlowLayout());
21: p2.add(new Button("first"));
22: p2.add(new Button("second"));
23: p2.add(new Button("third"));
24: p3 = new Panel();
25: //p3中设置组件
26: p3.setLayout(new GridLayout(3, 1));
27: p3.add(new Button("fourth"));
28: p3.add(new Button("fifth"));
29: p3.add(new Button("sixth"));
30: p4 = new Panel();
31: //p4中设置组件
32: p4.setLayout(new CardLayout());
33: p4.add("one", p2);
34: p4.add("two", p3);
35: ( (CardLayout) p4.getLayout()).show(p4, "one");
36: f.add(p4);
37: f.pack();
38: f.setVisible(true);
39:
40: f.addWindowListener(new WindowAdapter() {
41: public void windowClosing(WindowEvent e) {
42: System.exit(0);
43: }
44: });
45:
46: }
47:
48: public void actionPerformed(ActionEvent event) {
49: switch (i) {
50: case 1:
51: ( (CardLayout) p4.getLayout()).show(p4, "two");
52: break;
53: case 2:
54: ( (CardLayout) p4.getLayout()).show(p4, "one");
55: break;
56: }
57: i++;
58: if (i == 3) {
59: i = 1;
60: }
61: f.validate();
62: }
63:
64: public static void main(String args[]) {
65: new CardLayoutDemo();
66: }
67:
68: }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?