请教Swing的卡片布局,书上没有详细讲,我按书上的方法写了个程序,编译是成功的,程序运行不了,不知道怎么回事。
求各位大哥给我个卡片布局的例子。

解决方案 »

  1.   

    给你个例子:
    import java.awt.*;
    import java.awt.event.*;public class CardLayout1 extends Frame implements ActionListener {
    Panel pnl; Cvs cvs[]; Button btn1, btn2, btn3, btn4; CardLayout crd; CardLayout1() {
    super("Frame with CardLayout");
    setSize(300, 250);
    setBackground(Color.blue);
    setVisible(true);
    setLayout(null);
    crd = new CardLayout(15, 15);
    pnl = new Panel();
    pnl.setLayout(crd);
    pnl.setBackground(Color.yellow);
    add(pnl);
    btn1 = new Button("First");
    btn2 = new Button("Next");
    btn3 = new Button("Prev");
    btn4 = new Button("End");
    add(btn1);
    add(btn2);
    add(btn3);
    add(btn4);
    pnl.setBounds(20, 40, 200, 190);
    btn1.setBounds(230, 60, 50, 30);
    btn2.setBounds(230, 100, 50, 30);
    btn3.setBounds(230, 140, 50, 30);
    btn4.setBounds(230, 180, 50, 30);
    btn1.addActionListener(this);
    btn2.addActionListener(this);
    btn3.addActionListener(this);
    btn4.addActionListener(this); cvs = new Cvs[11];
    for (int i = 1; i <= 10; i++) {
    cvs[i] = new Cvs(i, i);
    pnl.add(" " + i, cvs[i]);
    } addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    setVisible(false);
    System.exit(0);
    }
    }); } public void actionPerformed(ActionEvent e) {
    if (e.getSource() == btn1) {
    crd.next(pnl);
    crd.first(pnl);
    } else if (e.getSource() == btn2)
    crd.next(pnl);
    else if (e.getSource() == btn3)
    crd.previous(pnl);
    else if (e.getSource() == btn4)
    crd.last(pnl);
    } class Cvs extends Canvas {
    int x, y; Cvs(int a, int b) {
    x = a;
    y = b;
    setBackground(Color.cyan);
    } public void paint(Graphics g) {
    g.setColor(Color.red);
    g.fillRect(50, 25, 10 * x, 8 * y);
    g.drawString("This is as " + x + " Map.", 20, 150);
    }
    } public static void main(String args[]) {
    CardLayout1 frm = new CardLayout1();
    }
    }
      

  2.   

    再来一个:
    import java.awt.*;
    import java.awt.event.*;public class CardLayoutDemo extends Frame {
    // 包含四个功能按钮的Panel的定义和创建
    Panel pnlCommandArea = new Panel(); // 显示功能Panel的定义和创建
    Panel pnlDisplayArea = new Panel(); // CardLayout布局管理器的创建
    CardLayout cardlayout1 = new CardLayout(); // 四个功能按钮的定义和创建
    Button btnFirst = new Button("第一个"); Button btnPrevious = new Button("前一个"); Button btnNext = new Button("后一个"); Button btnLast = new Button("最后一个"); // 框架窗体的构造方法
    public CardLayoutDemo() {
    // 设置Frame的布局管理器为BorderLayout
    this.setLayout(new BorderLayout());
    // 把两个Panel加入到布局管理器中
    this.add(pnlCommandArea, BorderLayout.NORTH);
    this.add(pnlDisplayArea, BorderLayout.CENTER);
    // =====================================================================
    // 显示功能区域
    // =====================================================================
    // 把显示功能区域Panel的布局管理器设置为CardLayout
    pnlDisplayArea.setLayout(cardlayout1);
    // 创建第一个显示Panel
    Panel pnlFirst = new Panel();
    pnlFirst.setBackground(Color.blue);
    pnlFirst.setForeground(Color.white);
    pnlDisplayArea.add("first", pnlFirst);
    pnlFirst.add(new Label("This is the first Panel"));
    // 创建第二个显示Panel
    Panel pnlSecond = new Panel();
    pnlSecond.setBackground(Color.red);
    pnlSecond.setForeground(Color.blue);
    pnlDisplayArea.add("second", pnlSecond);
    pnlSecond.add(new Label("This is the second Panel"));
    // 创建第三个显示Panel
    Panel pnlThird = new Panel();
    pnlThird.setBackground(Color.black);
    pnlThird.setForeground(Color.white);
    pnlDisplayArea.add("third", pnlThird);
    pnlThird.add(new Label("This is the third Panel"));
    // 创建第四个显示Panel
    Panel pnlFourth = new Panel();
    pnlFourth.setBackground(Color.green);
    pnlFourth.setForeground(Color.black);
    pnlDisplayArea.add("fourth", pnlFourth);
    pnlFourth.add(new Label("This is the fourth Panel"));
    // =====================================================================
    // 创建和显示功能按钮区域
    // =====================================================================
    // 为四个功能按钮设置事件监听器
    btnFirst.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    processAction(e);
    }
    });
    btnPrevious.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    processAction(e);
    }
    });
    btnNext.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    processAction(e);
    }
    });
    btnLast.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    processAction(e);
    }
    });
    // 把四个功能按钮加入到Panel
    pnlCommandArea.add(btnFirst);
    pnlCommandArea.add(btnPrevious);
    pnlCommandArea.add(btnNext);
    pnlCommandArea.add(btnLast);
    } // 程序的入口方法
    public static void main(String[] args) {
    // 创建框架窗体的实例
    CardLayoutDemo frmCardLayout = new CardLayoutDemo(); // 设置框架窗体的事件监听(关闭窗体事件)
    frmCardLayout.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    // 正常退出Java虚拟机
    System.exit(0);
    }
    }); // 显示框架窗体
    frmCardLayout.pack();
    frmCardLayout.show();
    } // 设置框架窗体的大小为300×300
    public Dimension getPreferredSize() {
    return new Dimension(300, 300);
    } // 处理按钮的事件
    private void processAction(ActionEvent e) {
    // 获取事件源(用户选择是哪个按钮)
    Button btnEvent = (Button) e.getSource(); if (btnEvent.equals(btnFirst))
    cardlayout1.first(pnlDisplayArea);
    else if (btnEvent.equals(btnLast))
    cardlayout1.last(pnlDisplayArea);
    else if (btnEvent.equals(btnPrevious))
    cardlayout1.previous(pnlDisplayArea);
    else if (btnEvent.equals(btnNext))
    cardlayout1.next(pnlDisplayArea);
    }
    }
      

  3.   

    为什么你这个和我书上说的方法不一样啊,书上是用卡片布局管理器的show()方法显示的:
    CardLayout cc=new CardLayout();
    setLayout(cc);
    add("Options Card",options);
    cc.show(this,"Fact Card");
    书上主要是讲这以上四句,为什么楼上大哥你的程序和这不一样呢?如果按书上说的该怎么写呢?