/*
时间:2015年3月18日09:21:57
目的:测试嵌套布局的使用。
Panel 是最简单的容器类。应用程序可以将其他组件放在面板提供的空间内,这些组件包括其他面板。
面板的默认布局管理器是 FlowLayout 布局管理器。
面板的构造方法有两个:
public Panel(LayoutManager layout)创建一个具有指定布局管理器的面板
public Panel();使用默认的布局管理器创建面板,所有面板的默认布局管理器都是FlowLayout类。
布局管理器总结:
Frame是一个顶级窗口,Frame的缺省布局管理器是BorderLayout
Panel无法单独显示,必须添加到容器中
Panel的缺省布局管理器为FlowLayout
当把Panel作为一个组件添加到某个容器中去,该Panel仍然可以有自己的布局管理器。
使用布局管理器时,布局管理器负责各个组件的大小和位置,因此用户无法在这种情况下设置组件大小和位置属性,
如果使用使用Java语言提供的
setLocation(), setSize(), setBounds()等方法,则都会布局管理器覆盖。
如用用户确实需要亲自设置组件大小和位置,则应取消该容器的布局管理器,方法为:setLayout(null);
*/
import java.awt.*;
public class TestNestedLayout {
public static void main(String[] args) {
Frame f = new Frame("TestNEstedLayout");
f.setLocation(300, 400);
f.setSize(500, 500);
f.setBackground(new Color(204, 204, 255));
f.setLayout(new GridLayout(2, 1));
Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new BorderLayout());
Panel p3 = new Panel(new GridLayout(2, 1));
Panel p4 = new Panel(new GridLayout(2, 2));
p1.add(new Button("Button"), BorderLayout.WEST);
p3.add(new Button("Button"));
p3.add(new Button("Button"));
p1.add(p3, BorderLayout.CENTER);
p1.add(new Button("Button"), BorderLayout.EAST);
p2.add(new Button("Button"), BorderLayout.WEST);
p4.add(new Button("Button"));
p4.add(new Button("Button"));
p4.add(new Button("Button"));
p4.add(new Button("Button"));
p2.add(p4, BorderLayout.CENTER);
p2.add(new Button("Button"), BorderLayout.EAST);
f.add(p1);
f.add(p2);
f.setVisible(true);
}
}
原文地址:http://blog.csdn.net/lk142500/article/details/44401983