Java AWT
Java AWT
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.
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.
Panel
The Panel is the container that doesn't contain title bar and menu bars. It can have other
components like button, textfield etc.
Frame
The Frame is the container that contain title bar and can have menu bars. It can have other
components like button, textfield etc.
public void setSize(int width,int height) sets the size (width and height) of the component.
Applet is a special type of program that is embedded in the webpage to generate the dynamic
content. It runs inside the browser and works at client side.
There are some important differences between an applet and a standalone Java application,
including the following −
Advantage of Applet
Drawback of Applet
1. Applet is initialized.
2. Applet is started.
3. Applet is painted.
4. Applet is stopped.
5. Applet is destroyed.
Lifecycle methods for Applet:
The java.applet.Applet class 4 life cycle methods and java.awt.Component class provides 1
life cycle methods for an applet.
java.applet.Applet class
For creating any applet java.applet.Applet class must be inherited. It provides 4 life cycle
methods of applet.
1. public void init(): is used to initialized the Applet. It is invoked only once. It is called
after the param tags inside the applet tag have been processed.
2. public void start(): is invoked after the init() method or browser is maximized. It is
used to start the Applet. It is also called whenever the user returns to the page
containing the applet after having gone off to other pages.
3. public void stop(): is used to stop the Applet. It is invoked when Applet is stop or
browser is minimized. This method is automatically called when the user moves off
the page on which the applet sits. It can, therefore, be called repeatedly in the same
applet.
4. public void destroy(): is used to destroy the Applet. It is invoked only once. This
method is only called when the browser shuts down normally. Because applets are
meant to live on an HTML page, you should not normally leave resources behind
after a user leaves the page that contains the applet.
java.awt.Component class
1. By html file.
2. By appletViewer tool (for testing purpose).
To execute the applet by html file, create an applet and compile it. After that create an html
file and place the applet code in html file. Now click the html file.
HelloWorld.html
<html>
<body>
<applet code="HelloWorld.class" width="300" height="300">
</applet>
</body>
</html>
Explanation:
1. The above java program begins with two import statements. The first import
statement imports the Applet class from applet package. Every AWT-based(Abstract
Window Toolkit) applet that you create must be a subclass (either directly or
indirectly) of Applet class. The second statement import the Graphics class from
AWT package.
2. The next line in the program declares the class HelloWorld. This class must be
declared as public because it will be accessed by code that is outside the program.
Inside HelloWorld, paint( ) is declared. This method is defined by the AWT and must
be overridden by the applet.
3. Inside paint( ) is a call to drawString( ), which is a member of the Graphics class.
This method outputs a string beginning at the specified X,Y location. It has the
following general form:
4. void drawString(String message, int x, int y)
Here, message is the string to be output beginning at x,y. In a Java window, the upper-
left corner is location 0,0. The call to drawString( ) in the applet causes the message
“Hello World” to be displayed beginning at location 20,20.
To execute the applet by appletviewer tool, create an applet that contains applet tag in
comment and compile it. After that run it by: appletviewer First.java. Now Html file is not
required but it is for testing purpose only.
//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
public void paint(Graphics g){
g.drawString("welcome to applet",150,150);
}
}
/*
<applet code="First.class" width="300" height="300">
</applet>
*/
c:\>javac First.java
c:\>appletviewer First.java