AWT布局管理器

布局管理器

容器内可以存放各种组件,而组件的位置和大小是由容器内的布局管理器来决定的。在AWT中为我们提供了以下5种布局管理器:

①   FlowLayout 流式布局管理器

②   BorderLayout 边界布局管理器

③   GridLayout 网格布局管理器

④   CradLayout 卡片布局管理器

⑤   GridBagLayout 网格包布局管理器

容器中组件的布局通常由布局管理器控制。每个Container(比如一个Panel或一个Frame)都有一个与他相关的缺省布局管理器,Panel容器默认的是FlowLayout,Frame容器默认的是BorderLayout,我们可以通过调用setLayout()来改变布局管理器;

可以通过设置空布局管理器,来控制组件的大小金和位置。可以调用setLayout(null)。

在设置空布局管理器后,必须对所有的组件调用setLocation(),setSize()或setBounds(),将它们定位在容器中。

流式布局管理器

 class MyFrame3 extends Frame {
public MyFrame3(String title) {
super(title);
} public void init() {
FlowLayout layout=new FlowLayout(FlowLayout.LEFT);//设置左对齐
this.setLayout(layout);
this.setBackground(Color.CYAN);
this.add(new Button("btn1"));
this.add(new Button("btn2"));
this.add(new Button("btn3"));
this.add(new Button("btn4"));
this.add(new Button("btn5"));
this.add(new Button("btn6"));
this.add(new Button("btn7"));
this.add(new Button("btn8"));
this.setSize(300, 300);
this.setVisible(true);
}
}

AWT布局管理器

边界布局管理器

 class MyFrame4 extends Frame {
public MyFrame4(String title) {
super(title);
} public void init() {
this.setBackground(Color.CYAN);
this.add(new Button("btn1"),BorderLayout.EAST);
this.add(new Button("btn2"),BorderLayout.WEST);
this.add(new Button("btn3"),BorderLayout.NORTH);
this.add(new Button("btn4"),BorderLayout.SOUTH);
this.add(new Button("btn5"),BorderLayout.CENTER);
this.setSize(300, 300);
this.setVisible(true);
}
}

AWT布局管理器

网格布局管理器

 class MyFrame5 extends Frame {
public MyFrame5(String title) {
super(title);
} public void init() {
GridLayout layout=new GridLayout(3,2);//创建一个3行2列的网格
this.setLayout(layout);
this.setBackground(Color.CYAN);
this.add(new Button("btn1"));
this.add(new Button("btn2"));
this.add(new Button("btn3"));
this.add(new Button("btn4"));
this.add(new Button("btn5"));
this.setSize(300, 300);
this.setVisible(true);
}
}

AWT布局管理器

卡片布局管理器

 class MyFrame6 extends Frame {
private Panel cardPanel=null;
private Panel ctrolPanel=null;
private CardLayout cardLayout=null;
private FlowLayout flowLayout=null;
private Label lb1,lb2,lb3,lb4;
private Button btnFirst,btnPrevious,btnNext,btnLast;
private TextField txtContent;
public MyFrame6(String title) {
super(title);
} public void init() {
//创建2个面板容器
cardPanel=new Panel();
ctrolPanel=new Panel(); //创建2个布局管理器
cardLayout=new CardLayout();
flowLayout=new FlowLayout(); //给容器设置指定的布局管理器
cardPanel.setLayout(cardLayout);//卡片容器中放置卡片布局
ctrolPanel.setLayout(flowLayout);//控制容器放置流式布局 //声明创建4个标签控件和一个文本框控件
lb1=new Label("第一页内容",Label.CENTER);
lb2=new Label("第二页内容",Label.CENTER);
txtContent=new TextField();//编辑文本框
lb3=new Label("第四页内容",Label.CENTER);
lb4=new Label("第五页内容",Label.CENTER); //构建四个按钮对象
btnFirst=new Button("第一张");
btnPrevious=new Button("上一张");
btnNext=new Button("下一张");
btnLast=new Button("最后一张");
ctrolPanel.add(btnFirst);
ctrolPanel.add(btnPrevious);
ctrolPanel.add(btnNext);
ctrolPanel.add(btnLast); //把四个标签控件和一个文本框控件添加到卡片容器中
cardPanel.add(lb1);
cardPanel.add(lb2);
cardPanel.add(txtContent);
cardPanel.add(lb3);
cardPanel.add(lb4);
this.add(cardPanel,BorderLayout.CENTER);//将卡片容器放在中部
this.add(ctrolPanel,BorderLayout.SOUTH);//将控制容器放在南部
this.setSize(400, 300);
this.setVisible(true);
}
}

AWT布局管理器

按钮事件的实现请查看下一篇...

上一篇:.net程序部署(setupFactory)


下一篇:HTML 表单和输入