📜  SWING-布局

📅  最后修改于: 2020-11-10 04:48:39             🧑  作者: Mango


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

布局经理

布局管理器自动将所有组件放置在容器内。即使您不使用布局管理器,组件仍将由默认布局管理器定位。可以手动布置控件,但是由于以下两个原因,这变得非常困难。

  • 处理容器内的大量控件非常繁琐。

  • 通常,当我们需要排列组件的宽度和高度信息时,不会给出它们。

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布局管理器类

以下是使用AWT设计GUI时常用的控件列表。

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 layout the components in a directional flow.

4 GridLayout

The GridLayout manages the components in the 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 the same size.

6 GroupLayout

The GroupLayout hierarchically groups the components in order to position them in a Container.

7 SpringLayout

A SpringLayout positions the children of its associated container according to a set of constraints.