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

📄 cardtest.java

📁 JAVA编程百例书中各章节的所有例子的源代码,包括套接字编程
💻 JAVA
字号:
package ch02.section03;

import java.applet.*;

import java.awt.*;
import java.awt.event.*;

class CardPanel
    extends Panel {
  ActionListener listener;

  Panel create(LayoutManager layout) {
    Button b = null;
    Panel p = new Panel();
    //设置布局方式
    p.setLayout(layout);

    b = new Button("one");
    //添加事件监听
    b.addActionListener(listener);
    //将b按钮放在容器的上边
    p.add("North", b);

    b = new Button("two");
    b.addActionListener(listener);
    //将b按钮放在容器的左边
    p.add("West", b);

    b = new Button("three");
    b.addActionListener(listener);
    //将b按钮放在容器的下边
    p.add("South", b);

    b = new Button("four");
    b.addActionListener(listener);
    //将b按钮放在容器的右边
    p.add("East", b);

    b = new Button("five");
    b.addActionListener(listener);
    //将b按钮放在容器的中间
    p.add("Center", b);

    b = new Button("six");
    b.addActionListener(listener);
    p.add("Center", b);

    return p;
  }

  CardPanel(ActionListener actionListener) {
    listener = actionListener;
    //设置布局方式为CardLayout
    setLayout(new CardLayout());
    add("one", create(new FlowLayout()));
    add("two", create(new BorderLayout()));
    add("three", create(new GridLayout(2, 2)));
    add("four", create(new BorderLayout(10, 10)));
    add("five", create(new FlowLayout(FlowLayout.LEFT, 10, 10)));
    add("six", create(new GridLayout(2, 2, 10, 10)));
  }

  public Dimension getPreferredSize() {
    return new Dimension(200, 100);
  }
}

//定义一个Applet
public class CardTest
    extends Applet
    implements ActionListener,
    ItemListener {
  CardPanel cards;

  public CardTest() {
    //设置布局方式为BorderLayout
    setLayout(new BorderLayout());
    //将一个容器放在中间
    add("Center", cards = new CardPanel(this));
  }

  public void itemStateChanged(ItemEvent e) {
    ( (CardLayout) cards.getLayout()).show(cards,
                                           (String) (e.getItem()));
  }

//定义一个按钮被点击的方法
  public void actionPerformed(ActionEvent e) {
    //获取事件源,返回一个字符串
    String arg = e.getActionCommand();
    /*
     CardLayout中的第几容器被返回
     CardLayout布局方式会把加入的组件当作“卡片”一样,
     使用者一次只能看到最上面的那张卡片,如果想要看到你所
     想要的卡片,则必须把那张卡片放到最上面来。
     */
    if ("first".equals(arg)) {
      /*
             方法first(Container parent)作用是翻到第一张卡片,
             parent 指的是有将此CardLayout设为layout manager的容器组件
       */
      ( (CardLayout) cards.getLayout()).first(cards);
    }
    else if ("next".equals(arg)) {
      /*
             方法next(Container parent)作用是翻到下一张卡片,
             如果是最后一张,则会翻到第一张
       */
      ( (CardLayout) cards.getLayout()).next(cards);
    }
    else if ("previous".equals(arg)) {
      /*
             方法next(Container parent)作用是翻到前一张卡片,
             如果是第一张,则会翻到最后一张
       */
      ( (CardLayout) cards.getLayout()).previous(cards);
    }
    else if ("last".equals(arg)) {
      /*
             方法next(Container parent)作用是翻到最后一张卡片
       */

      ( (CardLayout) cards.getLayout()).last(cards);
    }
    else {
      ( (CardLayout) cards.getLayout()).show(cards, (String) arg);
    }
  }

  public static void main(String args[]) {
    Frame f = new Frame("CardTest");
    /*
     添加关闭事件监听
     */
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    CardTest cardTest = new CardTest();
    //Applet启动
    cardTest.init();
    cardTest.start();
    //将组件放在Frame的中间
    f.add("Center", cardTest);
    //设置Frame窗口大小
    f.setSize(300, 300);
    //显示Frame
    f.show();
  }

  public String getAppletInfo() {
    return "Demonstrates the different types of layout managers.";
  }
}

⌨️ 快捷键说明

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