0% found this document useful (0 votes)
32 views8 pages

Unit 3 Introducing The AWT

The document discusses the Abstract Window Toolkit (AWT) in Java. It provides an overview of AWT classes like Component, Container, and Frame. It also describes how to create Frame windows by extending the Frame class and instantiating it. Examples are given of creating Frame windows by both approaches.

Uploaded by

Hari Thapa
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)
32 views8 pages

Unit 3 Introducing The AWT

The document discusses the Abstract Window Toolkit (AWT) in Java. It provides an overview of AWT classes like Component, Container, and Frame. It also describes how to create Frame windows by extending the Frame class and instantiating it. Examples are given of creating Frame windows by both approaches.

Uploaded by

Hari Thapa
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/ 8

https://genuinenotes.

com
Unit-3/ Java Programming-II

Unit 3: Introducing the AWT


 The Abstract Window Toolkit (AWT) was Java’s first GUI framework, and it has been part
of Java since version 1.0. It contains numerous classes and methods that allow you to create
windows and simple controls.
 AWT contains large number of classes and methods that allows us to create and manage
graphical user interface ( GUI ) applications, such as windows, buttons, scroll bars, etc.

AWT Classes
The AWT classes are contained in the java.awt package. It is one of Java’s largest packages.
The AWT package contains different classes :
 GUI Component classes, such as Button, TextField, and Label.
 GUI Container classes, such as Frame and Panel.
 Layout managers, such as FlowLayout, BorderLayout and GridLayout.
 Event Handling Classes, such as AWTEvent, AWTEventMultiCaster
 Custom graphics classes, such as Graphics, Color and Font
#AWT Classes are rarely used now days because of its platform dependent and heavy weight
nature.
Hierarchy of AWT classes:

1 Collected by Bipin Timalsina

https://genuinenotes.com
https://genuinenotes.com
Unit-3/ Java Programming-II

Window Fundamentals
 The AWT defines windows according to a class hierarchy that adds functionality and
specificity with each level.
 The two most common windows are those derived from Panel, which is used by applets,
and those derived from Frame, which creates a standard application window.
Component
At the top of the AWT hierarchy is the Component class. Component is an abstract class that
encapsulates all of the attributes of a visual component. Except for menus, all user interface
elements that are displayed on the screen and that interact with the user are subclasses of
Component. A Component object is responsible for remembering the current foreground and
background colors and the currently selected text font.
 Components are elementary GUI entities, such as Button, Label, and TextField.

Container
The Container class is a subclass of Component. It has additional methods that allow other
Component objects to be nested within it. Other Container objects can be stored inside of a
Container (since they are themselves instances of Component). This makes for a multileveled
containment system. A container is responsible for laying out (that is, positioning) any
components that it contains. It does this through the use of various layout managers.
 Containers, such as Frame and Panel, are used to hold components in a specific layout (such
as FlowLayout or GridLayout). A container can also hold sub-containers.

 Each GUI program has a top-level container. The commonly-used top-level containers in
AWT are Frame, Dialog and Applet.
 Secondary containers are placed inside a top-level container or another secondary container.
(Panel, ScrollPane)
Panel
The Panel class is a concrete subclass of Container. Panel is the superclass for Applet. When
screen output is directed to an applet, it is drawn on the surface of a Panel object. In essence, a
Panel is a window that does not contain a title bar, menu bar, or border.

2 Collected by Bipin Timalsina

https://genuinenotes.com
https://genuinenotes.com
Unit-3/ Java Programming-II

Window
The Window class creates a top-level window. A top-level window is not contained within any
other object; it sits directly on the desktop. Generally, you won’t create Window objects directly.
Instead, you will use a subclass of Window called Frame
Frame
Frame encapsulates what is commonly thought of as a “window.” It is a subclass of Window and
has a title bar, menu bar, borders, and resizing corners.
 A Frame provides the "main window" for your GUI application. It has a title bar (containing
an icon, a title, the minimize, maximize/restore-down and close buttons), an optional menu
bar, and the content display area.

Canvas
Although it is not part of the hierarchy for applet or frame windows, there is one other type of
window that we will find valuable: Canvas. Derived from Component, Canvas encapsulates a
blank window upon which we can draw.

Working with Frame Windows


 There are two ways to create a Frame. They are,
o By Instantiating Frame class
o By extending Frame class
 Commonly used constructors for creating frames are:
 Frame( ): creates a standard window that does not contain a title
 Frame (String title): creates a window with the title specified by title.

3 Collected by Bipin Timalsina

https://genuinenotes.com
https://genuinenotes.com
Unit-3/ Java Programming-II

Setting the Window’s Dimensions


The setSize( ) method is used to set the dimensions of the window. Its signature is shown here:
void setSize(int newWidth, int newHeight)
void setSize(Dimension newSize)

The new size of the window is specified by newWidth and newHeight, or by the
width and height fields of the Dimension object passed in newSize. The
dimensions are specified in terms of pixels.
The getSize( ) method is used to obtain the current size of a window. One of its forms is
shown here:
Dimension getSize( )
This method returns the current size of the window contained within the width and height fields
of a Dimension object.
Hiding and Showing a Window
After a frame window has been created, it will not be visible until you call setVisible( ). Its
signature is shown here:
void setVisible(boolean visibleFlag)
The component is visible if the argument to this method is true. Otherwise, it is hidden.
Setting a Window’s Title
You can change the title in a frame window using setTitle( ), which has this general form:
void setTitle(String newTitle)
Here, newTitle is the new title for the window.
Closing a Frame Window
When using a frame window, your program must remove that window from the screen when it is
closed, by calling setVisible(false). For this event handling should be implemented.

4 Collected by Bipin Timalsina

https://genuinenotes.com
https://genuinenotes.com
Unit-3/ Java Programming-II

Example of AWT Frame by inheritance

package awtexample;
import java.awt.*;

/* We have extended the Frame class here,


* thus our class "FirstFrame" would behave like a Frame
* @author BIPIN
*/
public class FirstFrame extends Frame{
FirstFrame(){
Button b=new Button("Click"); //creating button
b.setBounds(50, 100, 60, 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
setTitle("First Frame");//title of frame
setVisible(true);//now frame will be visible, by default not
visible
}
public static void main(String args[]){
FirstFrame f1=new FirstFrame();
}
}

5 Collected by Bipin Timalsina

https://genuinenotes.com
https://genuinenotes.com
Unit-3/ Java Programming-II

Example of AWT Frame by instantiating Frame class (creating object of Frame)


package awtexample;
import java.awt.*;
/*
* creating Frame by creating instance of Frame class
* @author BIPIN
*/
public class SecondFrame {
SecondFrame(){
Frame fm=new Frame("Second Frame"); //Creating a frame with title
Label lb = new Label("welcome to java GUI");//Creating a label
fm.add(lb); //adding label to the frame.
fm.setSize(300, 300); //setting frame size.
fm.setVisible(true); //set frame visibilty true.
}
public static void main(String args[])
{
SecondFrame f2 = new SecondFrame();
}
}

While it is possible to simply create a window by creating an instance of Frame, it is not so useful
to do so, because you will not be able to do much with it. For example, you will not be able to
receive or process events that occur within it or easily output information to it. Most of the time,
you will create a subclass of Frame. Doing so lets you override Frame’s methods and provide event
handling.

Creating a frame window in an applet


Creating a new frame window from within an AWT-based applet is actually quite easy.
 First, create a subclass of Frame.
 Next, override any of the standard applet methods, such as init( ), start( ), and stop( ),
to show or hide the frame as needed.

6 Collected by Bipin Timalsina

https://genuinenotes.com
https://genuinenotes.com
Unit-3/ Java Programming-II

 Finally, implement the windowClosing( ) method of the WindowListener interface,


calling setVisible(false) when the window is closed. (It means by handling window
event)
Once we have defined a Frame subclass, we can create an object of that class. This causes a
frame window to come into existence, but it will not be initially visible. We make it visible by
calling setVisible( ).
When created, the window is given a default height and width. We can set the size of the window
explicitly by calling the setSize( ) method
Example:
package awtexample;
import java.applet.Applet;
import java.awt.*;
public class AppletFrame extends Applet {
/*<applet code="AppletFrame" width=500 height=500></applet>*/
Frame f;
public void init() {
f = new Frame ("A Frame Window");
f.setSize(300, 200);
f.setVisible(true);
}
public void start(){
f.setVisible(true);
}
public void stop(){
f.setVisible(false);
}
public void paint(Graphics g){
g.drawString("This is an applet Window",25,50);
}
}

7 Collected by Bipin Timalsina

https://genuinenotes.com
https://genuinenotes.com
Unit-3/ Java Programming-II

Output :

8 Collected by Bipin Timalsina

https://genuinenotes.com

You might also like