⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cardlayoutdemo.java

📁 JAVA的一些基础教程
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;

public class CardLayoutDemo implements ActionListener {
  Panel p1, p2, p3, p4;
  int i = 1;
  Frame f;

  public CardLayoutDemo() {
    f = new Frame("cardLayoutDemo");
    f.setLayout(new GridLayout(2, 1));
    p1 = new Panel();
    Button b = new Button("change card");
    b.addActionListener(this);
    p1.add(b);
    f.add(p1);

    p2 = new Panel();
    //p2中设置组件
    p2.setLayout(new FlowLayout());
    p2.add(new Button("first"));
    p2.add(new Button("second"));
    p2.add(new Button("third"));
    p3 = new Panel();
    //p3中设置组件
    p3.setLayout(new GridLayout(3, 1));
    p3.add(new Button("fourth"));
    p3.add(new Button("fifth"));
    p3.add(new Button("sixth"));
    p4 = new Panel();
    //p4中设置组件
    p4.setLayout(new CardLayout());
    p4.add("one", p2);
    p4.add("two", p3);
    ( (CardLayout) p4.getLayout()).show(p4, "one");
    f.add(p4);
    f.pack();
    f.setVisible(true);

    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

  }

  public void actionPerformed(ActionEvent event) {
    switch (i) {
      case 1:
        ( (CardLayout) p4.getLayout()).show(p4, "two");
        break;
      case 2:
        ( (CardLayout) p4.getLayout()).show(p4, "one");
        break;
    }
    i++;
    if (i == 3) {
      i = 1;
    }
    f.validate();
  }

  public static void main(String args[]) {
    new CardLayoutDemo();
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -