Swing 简明教程
SWING - Layouts
布局是指容器内组件的排列。从另一个角度来说,也可以说布局是在容器内将组件放置在特定位置。放置控件的任务由布局管理器自动完成。
Layout refers to the arrangement of components within the container. In another way, it could be said that layout is placing the components at a particular position within the container. The task of laying out the controls is done automatically by the Layout Manager.
Layout Manager
布局管理器自动定位容器内的所有组件。即使您不使用布局管理器,组件仍然会由默认布局管理器定位。动手放置控件是可能的,但是由于以下两个原因,这变得非常困难。
The layout manager automatically positions all the components within the container. Even if you do not use the layout manager, the components are still positioned by the default layout manager. It is possible to lay out the controls by hand, however, it becomes very difficult because of the following two reasons.
-
It is very tedious to handle a large number of controls within the container.
-
Usually, the width and height information of a component is not given when we need to arrange them.
Java 提供了各种布局管理器来定位控件。诸如大小、形状和排列之类的属性在不同的布局管理器之间各不相同。当 applet 或应用程序窗口的大小改变时,组件的大小、形状和排列也会相应地改变,即布局管理器适应 applet 查看器或应用程序窗口的尺寸。
Java provides various layout managers to position the controls. Properties like size, shape, and arrangement varies from one layout manager to the other. When the size of the applet or the application window changes, the size, shape, and arrangement of the components also changes in response, i.e. the layout managers adapt to the dimensions of the appletviewer or the application window.
布局管理器与容器对象相关联。每个布局管理器都是实现 LayoutManager 接口的类的对象。
The layout manager is associated with every Container object. Each layout manager is an object of the class that implements the LayoutManager interface.
以下是定义布局管理器功能的接口。
Following are the interfaces defining the functionalities of Layout Managers.
Sr.No. |
Interface & Description |
1 |
LayoutManagerThe LayoutManager interface declares those methods which need to be implemented by the class, whose object will act as a layout manager. |
2 |
LayoutManager2The 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 Layout Manager Classes
以下是使用 AWT 设计 GUI 时常用的控件列表。
Following is the list of commonly used controls while designing GUI using AWT.
Sr.No. |
LayoutManager & Description |
1 |
BorderLayoutThe borderlayout arranges the components to fit in the five regions: east, west, north, south, and center. |
2 |
CardLayoutThe CardLayout object treats each component in the container as a card. Only one card is visible at a time. |
3 |
FlowLayoutThe FlowLayout is the default layout. It layout the components in a directional flow. |
4 |
GridLayoutThe GridLayout manages the components in the form of a rectangular grid. |
5 |
GridBagLayoutThis 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 |
GroupLayoutThe GroupLayout hierarchically groups the components in order to position them in a Container. |
7 |
SpringLayoutA SpringLayout positions the children of its associated container according to a set of constraints. |