窗口布局
把上层窗口称为容器(Container)
容器里可以有多个子窗口或子空间(Component)
所谓布局,就是决定每一个子空间显示在什么地方
窗口坐标
每个矩形区域 Rectangle 的参数:
左上角坐标(x,y)
宽度:width
高度:height
取消面板布局器,使用setBounds进行定位和确定大小:
public MyFrame(String title) {
super(title);
//不使用布局器(默认地,ContentPane自带一个BorderLayout布局器)
Container root = this.getContentPane();
root.setLayout(null);
//由于没有布局器,所以默认地,子控件无法显示
JLabel a1 = new ColorfulLabel("1", Color.YELLOW);
JLabel a2 = new ColorfulLabel("2", Color.BLUE);
a1.setBounds(new Rectangle(0, 0, 100, 100));
a2.setBounds(new Rectangle(100, 100, 200, 200));
root.add(a1);
root.add(a2);
}
要点和细节:
- 取消布局器后,子空间默认是不显示的,用setBounds确定位置和大小后显示
- Frame窗口大小,包括了标题栏,所以下面的有效区域不足300像素。