介绍

布局是指容器内组件的布置。换句话说,可以说将组件放置在容器内的特定位置。布局控件的任务由布局管理器自动完成。

布局管理器

布局管理器自动将所有组件放置在容器中。如果我们不使用布局管理器,那么组件也将通过默认布局管理器进行定位。可以手动布置控件,但是由于以下两个原因,这变得非常困难。

  • 处理容器内的大量控件非常繁琐。
  • 当我们需要排列它们的宽度和高度信息时,通常不会给出它们。

Java为我们提供了各种布局管理器来定位控件。大小,形状和排列之类的属性在一个布局管理器之间会有所不同。当applet或应用程序窗口的大小改变时,组件的形状和排列也随之改变,即布局管理器适应appletviewer或应用程序窗口的大小。

布局管理器与每个Container对象关联。每个布局管理器都是实现LayoutManager接口的类的对象。

以下是定义布局管理器功能的接口。

Sr. No.Interface & Description
1

LayoutManager

The LayoutManager interface declares those methods which need to be implemented by the class whose object will act as a layout manager.

2

LayoutManager2

The LayoutManager2 is the sub-interface of the LayoutManager.This interface is for those classes that know how to layout containers based on layout constraint object.

AWT布局管理器类:

Following is the list of commonly used controls while designed GUI using AWT.

Sr. No.LayoutManager & Description
1

BorderLayout

The borderlayout arranges the components to fit in the five regions: east, west, north, south and center.

2

CardLayout

The CardLayout object treats each component in the container as a card. Only one card is visible at a time.

3

FlowLayout

The FlowLayout is the default layout.It layouts the components in a directional flow.

4

GridLayout

The GridLayout manages the components in form of a rectangular grid.

5

GridBagLayout

This is the most flexible layout manager class.The object of GridBagLayout aligns the component vertically,horizontally or along their baseline without requiring the components of same size.