Java编程使用卡片布局管理器示例【基于swing组件】

时间:2022-08-31 19:22:27

本文实例讲述了java编程使用卡片布局管理器。分享给大家供大家参考,具体如下:

运行效果:

Java编程使用卡片布局管理器示例【基于swing组件】

完整示例代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.han;
import java.awt.borderlayout;
import java.awt.cardlayout;
import java.awt.container;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import javax.swing.jbutton;
import javax.swing.jframe;
import javax.swing.jlabel;
import javax.swing.jpanel;
import javax.swing.swingconstants;
/**
 * 使用卡片布局管理器
 * @author han
 *
 */
public class cardlayout_1 extends jframe {
  /**
   *
   */
  private static final long serialversionuid = -5255525755443566487l;
  public cardlayout_1() {
    // todo auto-generated constructor stub
    container container = getcontentpane();
    final cardlayout cardlayout = new cardlayout();
    final jpanel cardpanel = new jpanel(cardlayout);
    string[] cardnames = { "卡片a", "卡片b", "卡片c" };
    for (int i = 0; i < cardnames.length; i++) {
//     jlabel label = new jlabel(cardnames[i]);
      jlabel label = new jlabel(cardnames[i], swingconstants.center);
//     cardpanel.add(label);
      cardpanel.add(label, cardnames[i]);
    }
    container.add(cardpanel, borderlayout.center);
    jpanel buttonpanel = new jpanel(); // by default, the flow layout
    container.add(buttonpanel, borderlayout.south);
    string[] buttonnames = { "第一个", "前一个", "卡片a", "卡片b", "卡片c",
        "后一个", "最后一个" };
    for (int i = 0; i < buttonnames.length; i++) {
      final jbutton button = new jbutton(buttonnames[i]);
      buttonpanel.add(button);
      button.addactionlistener(new actionlistener() {
        @override
        public void actionperformed(actionevent e) {
          // todo auto-generated method stub
          string buttontext = button.gettext();
          if (buttontext.equals("第一个")) {
            cardlayout.first(cardpanel);
          } else if (buttontext.equals("前一个")) {
            cardlayout.previous(cardpanel);
          } else if (buttontext.equals("卡片a")) {
            cardlayout.show(cardpanel, "卡片a");
          } else if (buttontext.equals("卡片b")) {
            cardlayout.show(cardpanel, "卡片b");
          } else if (buttontext.equals("卡片c")) {
            cardlayout.show(cardpanel, "卡片c");
          } else if (buttontext.equals("后一个")) {
            cardlayout.next(cardpanel);
          }else {
            cardlayout.last(cardpanel);
          }
        }
      });
    }
  }
  /**
   * @param args
   */
  public static void main(string[] args) {
    // todo auto-generated method stub
    cardlayout_1 frame = new cardlayout_1();
    frame.settitle("www.zzvips.com - 使用卡片布局管理器");
    frame.setvisible(true);
    frame.setdefaultcloseoperation(jframe.exit_on_close);
    frame.pack();
  }
}

希望本文所述对大家java程序设计有所帮助。

原文链接:http://blog.csdn.net/gaowen_han/article/details/7738643