Containers and Components
There are two types of GUI elements:
- Component: Components are elementary GUI entities (such as
- Container: Containers (such as
Applet) are used to hold components in a specific layout (such as flow or grid). A container can also hold sub-containers.
GUI components are also called controls (Microsoft ActiveX Control), widgets (Eclipse’s Standard Widget Toolkit, Google Web Toolkit), which allow users to interact with (i.e., control) the application through these components (such as button-click and text-entry).
In the above figure, there are three containers: a
Frame and two
Frame is the top-level container of an AWT program. A
Frame has a title bar (containing an icon, a title, and the minimize/maximize/close buttons), an optional menu bar and the content display area. A
Panel is a rectangular area used to group related GUI components in a certain layout. In the above figure, the top-level
Frame contains two
Panels. There are five components: a
Label (providing description), a
TextField (for users to enter text), and three
Buttons (for user to trigger certain programmed actions).
In a GUI program, a component must be kept in a container. You need to identify a container to hold the components. Every container has a method called
add(Component c). A
aContainer) can invoke
aContainer.add(aComponent) to add
aComponent into itself. For example,
Panel panel = new Panel(); // Panel is a Container Button btn = new Button("Press"); // Button is a Component panel.add(btn); // The Panel Container adds a Button Component