Java AWT
Java AWT
The applet life cycle can be defined as the process of how the object is
created, started, stopped, and destroyed during the entire execution of its
application. It basically has five core methods namely init(), start(), stop(),
paint() and destroy().These methods are invoked by the browser to execute.
Along with the browser, the applet also works on the client side, thus
having less processing time.
There are five methods of an applet life cycle, and they are:
o init(): The init() method is the first method to run that initializes the
applet. It can be invoked only once at the time of initialization. The
web browser creates the initialized objects, i.e., the web browser (after
checking the security settings) runs the init() method within the
applet.
o start(): The start() method contains the actual code of the applet and
starts the applet. It is invoked immediately after the init() method is
invoked. Every time the browser is loaded or refreshed, the start()
method is invoked. It is also invoked whenever the applet is
maximized, restored, or moving from one tab to another in the
browser. It is in an inactive state until the init() method is invoked.
o stop(): The stop() method stops the execution of the applet. The stop
() method is invoked whenever the applet is stopped, minimized, or
moving from one tab to another in the browser, the stop() method is
invoked. When we go back to that page, the start() method is invoked
again.
o destroy(): The destroy() method destroys the applet after its work is
done. It is invoked when the applet window is closed or when the tab
containing the webpage is closed. It removes the applet object from
memory and is executed only once. We cannot start the applet once
it is destroyed.
o paint(): The paint() method belongs to the Graphics class in Java. It is
used to draw shapes like circle, square, trapezium, etc., in the applet.
It is executed after the start() method and when the browser or applet
windows are resized.
1. init()
2. start()
3. paint()
1. import java.applet.Applet;
2. import java.awt.Graphics;
3. public class First extends Applet{
4.
5. public void paint(Graphics g){
6. g.drawString("welcome",150,150);
7. }
8.
9. }
c:\>javac First.java
c:\>appletviewer First.java
Java AWT (Abstract Window Toolkit) is an API to develop Graphical User
Interface (GUI) or windows-based applications in Java.
The AWT tutorial will help the user to understand Java GUI programming in
simple and easy steps.
Container
The Container is a component in AWT that can contain another
components like buttons, textfields, labels etc. The classes that extends
Container class are known as container such as Frame, Dialog and Panel.
It is basically a screen where the where the components are placed at their
specific locations. Thus it contains and controls the layout of components.
Types of containers:
1. Window
2. Panel
3. Frame
4. Dialog
Window
The window is the container that have no borders and menu bars. You must
use frame, dialog or another window for creating a window. We need to
create an instance of Window class to create this container.
Panel
The Panel is the container that doesn't contain title bar, border or menu
bar. It is generic container for holding the components. It can have other
components like button, text field etc. An instance of Panel class creates a
container, in which we can add components.
Frame
The Frame is the container that contain title bar and border and can have
menu bars. It can have other components like button, text field, scrollbar
etc. Frame is most widely used container while developing an AWT
application.
Method Description
public void setSize(int width,int Sets the size (width and height) of the
height) component.
AWTExample1.java
// no layout manager
setLayout(null);
// main method
public static void main(String args[]) {