Page 410 - CITS - Computer Software Application -TT
P. 410
COMPUTER SOFTWARE APPLICATION - CITS
Components and containers:-
A graphical user interface is built of graphical elements called components. Typical components include such
items as buttons, scrollbars, and text fields. Components allow the user to interact with the program and provide
the user with visual feedback about the state of the program. In the AWT, all user interface components are
instances of class Component or one of its subtypes.
Components do not stand alone, but rather are found within containers. Containers contain and control the layout
of components. Containers are themselves components, and can thus be placed inside other containers. In the
AWT, all containers are instances of class Container or one of its subtypes.
Spatially, components must fit completely within the container that contains them. This nesting of components
(including containers) into containers creates a tree of elements, starting with the container at the root of the tree
and expanding out to the leaves, which are components such as buttons.
Types of containers:-
The AWT provides four container classes. They are class Window and its two subtypes -- class Frame and
class Dialog -- as well as the Panel class. In addition to the containers provided by the AWT, the Applet class is
a container -- it is a subtype of the Panel class and can therefore hold components. Brief descriptions of each
container class provided by the AWT are provided below.
Window A top-level display surface (a window). An instance of the Window class is not attached to nor
embedded within another container. An instance of the Window class has no border and no title.
Frame A top-level display surface (a window) with a border and title. An instance of the Frame class may
have a menu bar. It is otherwise very much like an instance of the Window class.
Dialog A top-level display surface (a window) with a border and title. An instance of the Dialog class cannot
exist without an associated instance of the Frame class.
Panel A generic container for holding components. An instance of the Panel class provides a container to
which to add components.
Creating a container
Before adding the components that make up a user interface, the programmer must create a container. When
building an application, the programmer must first create an instance of class Window or class Frame. When
building an applet, a frame (the browser window) already exists. Since the Applet class is a subtype of the Panel
class, the programmer can add the components to the instance of the Applet class itself.
The code in Listing 1 creates an empty frame. The title of the frame (“Example 1”) is set in the call to the
constructor. A frame is initially invisible and must be made visible by invoking its show() method.
import java.awt.*;
public class Example1
{
public static void main(String [] args)
{
Frame f = new Frame(“Example 1”);
f.show();
}
}
Adding components to a container
To be useful, a user interface must consist of more than just a container -- it must contain components. Components
are added to containers via a container’s add() method. There are three basic forms of the add() method. The
method to use depends on the container’s layout manager (see the section titled Component layout).
397
CITS : IT&ITES - Computer Software Application - Lesson 116 - 119