JavaSE series code 44: Use of CardLayout class

Java does not support multiple inheritance, that is, a class can only have one parent class. Single inheritance makes Java simple and easy to manage programs. In order to overcome the disadvantage of single inheritance, Java uses interfaces, and a class can implement multiple interfaces.
Use the keyword interface to define an interface. The definition of an interface is very similar to that of a class. It can be divided into interface declaration and interface body.

import java.awt.*; 
public class Javase_44 extends Frame
{
  static Frame frm=new Frame("卡片式布局设置管理器CardLayout");
  static Panel pan1=new Panel();     //创建面板对象
  static Panel pan2=new Panel();
  public static void main(String[] args)
  {
    frm.setLayout(null);        //取消窗口的页面设置
    pan2.setLayout(new GridLayout(1,4)); //将面板对象设置为1行4列的布局
    CardLayout crd=new CardLayout (5,10);  //创建卡片式布局对象crd
    pan1.setLayout(crd);      //将面板pan1设置为卡片式布局方式
    frm.setSize(300,250);
    frm.setResizable(false);
    pan1.setBounds(10,20,270,200);
    pan2.setBounds(10,220,270, 20);
    frm.add(pan1);    //将面板添加到窗口里
    frm.add(pan2) ;
    Label lab1=new Label("第一页", Label.CENTER);
    TextField tex=new TextField("卡片式布局策略CardLayout",18);
    pan1.add(lab1, "c1");     //将标签组件lab1命名为c1后加入到面板中
    pan1.add(new Label("第二页", Label.CENTER), "c2");
    pan1.add(tex, "t1");      //将文本框组件tex命名为t1后加入到面板中
    crd.show(pan1, "t1");     //将pan1中的tex组件显示在容器中
    pan2.add(new Button("第一页"), "1");
    pan2.add(new Button("上一页"),"2");
    pan2.add(new Button("下一页"),"3");
    pan2.add(new Button("最后页"),"4");
    frm.setVisible(true);
  }
}
Published 52 original articles · Like 162 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/blog_programb/article/details/105394534