Documente Academic
Documente Profesional
Documente Cultură
Java AWT
Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or
window-based application in java.
Java AWT components are platform-dependent i.e. components are
displayed according to the view of operating system. AWT is
heavyweight i.e. its components uses the resources of system.
The java.awt package provides classes for AWT API such as
TextField, Label, TextArea, RadioButton, CheckBox, Choice, List etc.
Java AWT Hierarchy
The hierarchy of Java AWT classes are given below.
Component Class
Component class is at the top of AWT hierarchy.
Component class is an abstract class that encapsulates all of the
attributes of a visual component.
All user interface elements that are displayed on the screen and that
interact with the users are subclasses of component. If defines a
number of methods that are responsible for managing events.
Important methods of Component class
Following are the important methods, practiced very often, of Component class that can
be used by all the sub classes (components).
setEnabled(boolean): The parameter false makes the component disabled so that it does not
respond to user interactions (say, clicks).
setBounds(): Using this method, the programmer can give his own size to the component in terms
of width and height and also the location where he can place the component in the container. This
method is not used often as the programmer prefers to place the component in the container using
layout managers.
getWidth(): The programmer can obtain the width of the component.
getHeight(): The programmer can obtain the height of the component.
paint(): Used very extensively to draw graphics. This method is called implicitly when the frame is
created or resized.
repaint(): Used by the programmer to call the paint() method explicitly anywhere in the program.
setBackground(): Used to give a background color to a component.
setForeground(): Used to give a foreground color to a component.
setFont(): Used to give a font to a component to display the text.
getSize(): Returns the size of the component.
setSize(): Used to give the size to the component.
update(): The is method is called implicitly by the repaint(). A call to this method clears the earlier
drawings existing on the container.
Container class
The container class is a subclass of component class. It has additional
methods that allow other component objects to be nested in it.
A container is responsible for laying out any components that it
contains. It does this through the use of various layout managers.
Java AWT Hierarchy
The hierarchy of Java AWT classes are given below.
Important methods in Container class
Following are the important methods of the Container class.
add(): This method is overloaded with which a component can be
added to the container.
invalidate(): Used to invalidate the present set up of components
in the container.
validate(): Used to revalidate the current set up of components
after calling invalidate().
The important containers used very often are applets, frames. Rare are
dialog boxes.
Layout Managers
Each container has a layout manager associated with it.
The LayoutManagers are used to arrange components in a particular
manner. LayoutManager is an interface that is implemented by all the
classes of layout managers. There are following classes that represents
the layout managers:
java.awt.BorderLayout
java.awt.FlowLayout
java.awt.GridLayout
java.awt.CardLayout
java.awt.GridBagLayout
The layout manager is set by the setLayout() method. If no call is
made to setLayout(), then default layout manager is used.
Whenever a container is resized, the layout manager is used to position
each of the components within it.
FlowLayout(Default Layout)
The FlowLayout is used to arrange the components in a line, one after another (in a flow). It is the
default layout manager.
FlowLayout(): creates a flow layout with centered alignment and a default 5 pixels horizontal
and vertical gap.
FlowLayout(int align): creates a flow layout with the given alignment and a default 5 pixels
horizontal and vertical gap.
FlowLayout(int align, int hgap, int vgap): creates a flow layout with the given alignment and
the given horizontal and vertical gap.
The values passed in top, left, bottom and right specify the amount of
space between the container and its enclosing window.
Insets Example
import java.awt.*;
import java.applet.*;