[go: up one dir, main page]

0% found this document useful (0 votes)
14 views7 pages

The AWT Class Hierarchy

The document provides an overview of Java AWT (Abstract Window Toolkit) for GUI programming, detailing its class hierarchy, including components like Frame, Panel, and Applet. It explains the lifecycle of applets, their advantages and drawbacks, and includes examples of creating AWT applications and applets. Additionally, it covers methods for managing components and parameters in applets.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views7 pages

The AWT Class Hierarchy

The document provides an overview of Java AWT (Abstract Window Toolkit) for GUI programming, detailing its class hierarchy, including components like Frame, Panel, and Applet. It explains the lifecycle of applets, their advantages and drawbacks, and includes examples of creating AWT applications and applets. Additionally, it covers methods for managing components and parameters in applets.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

GUI Programming with java: The AWT class hierarchy, MVC architecture.

Applet Revisited:
Basics, architecture and skeleton, simple applet program.
The AWT Class hierarchy

• Java AWT (Abstract Window Toolkit) is an API to develop GUI or window-based


applications 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 are using the resources of OS.
• The java.awt package provides classes for AWT api such as TextField, Label,
TextArea, RadioButton, CheckBox, Choice, List etc.

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.

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.

Useful Methods of Component class


Method Description

public void add(Component c) inserts a component on this component.

public void setSize(int width,int sets the size (width and height) of the component.
height)

public void defines the layout manager for the component.


setLayout(LayoutManager m)

public void setVisible(boolean changes the visibility of the component, by default false.
status)

Java AWT Example

To create simple awt example, you need a frame. There are two ways to create a frame in
AWT.

o By extending Frame class (inheritance)


o By creating the object of Frame class (association)

AWT Example by Inheritance

Let's see a simple example of AWT where we are inheriting Frame class. Here, we are
showing Button component on the Frame.

import java.awt.*;
class First extends Frame{
First(){
Button b=new Button("click me");
b.setBounds(30,100,80,30);// setting button position
add(b);//adding button into frame
setSize(300,300);//frame size 300 width and 300 height
setLayout(null);//no layout manager
setVisible(true);//now frame will be visible, by default not visible
}
public static void main(String args[]){
First f=new First();
}}

The setBounds(int xaxis, int yaxis, int width, int height) method is used in the above example
that sets the position of the awt button.

AWT Example by Association

Let's see a simple example of AWT where we are creating instance of Frame class. Here, we
are showing Button component on the Frame.

import java.awt.*;
class First2{
First2(){
Frame f=new Frame();
Button b=new Button("click me");
b.setBounds(30,50,80,30);
f.add(b);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[]){
First2 f=new First2();
}}

Applet
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.

Advantage of Applet
• There are many advantages of applet they are,
• It works at client side so less response time.
• Secured
• It can be executed by browsers running under many platforms, including Linux,
Windows, Mac Os etc.

Drawback of Applet
• Plugin is required at client browser to execute applet.

Lifecycle of Java Applet


1.Applet is initialized.
2.Applet is started.
3.Applet is painted.
4.Applet is stopped.
5.Applet is destroyed.

Hierarchy of Applet

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.
2.public void start(): is invoked after the init() method or browser is maximized. It is used to
start the Applet.
3.public void stop(): is used to stop the Applet. It is invoked when Applet is stop or browser
is minimized.
4.public void destroy(): is used to destroy the Applet. It is invoked only once.
java.awt.Component class
The Component class provides 1 life cycle method of applet.

1. public void paint(Graphics g): is used to paint the Applet. It provides Graphics class object
that can be used for drawing oval, rectangle, arc etc.
Simple example of Applet by html file:

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.

//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{ public void paint(Graphics g){
g.drawString("welcome",150,150);
}
}

Simple example of Applet by appletviewer tool:

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>
*/
To execute the applet by appletviewer tool, write in command prompt
c:\>javac First.java
c:\>appletviewer First.java

Parameter in Applet
We can get any information from the HTML file as a parameter. For this purpose, Applet
class provides a method named getParameter().
Syntax:

1. public String getParameter(String parameterName)

Example of using parameter in Applet:

import java.applet.Applet;
import java.awt.Graphics;
public class UseParam extends Applet 4. {
public void paint(Graphics g)
{
String str=getParameter("msg");
g.drawString(str,50, 50);
}}
myapplet.html
<html>
<body>
<applet code="UseParam.class" width="300" height="300">
<param name="msg" value="Welcome to applet">
</applet>
</body>
</html>

You might also like