0% found this document useful (0 votes)
18 views

Ch1 - Creating GUI Using AWT Components

The document discusses Java AWT components and how to create a GUI using them. It describes key classes like Frame, Panel, Checkbox, Choice, Label and Scrollbar. It provides examples of creating each component and adding them to containers like Frame and Panel. Code snippets and output screenshots are included to demonstrate creating a GUI with these AWT components.

Uploaded by

moheda214
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Ch1 - Creating GUI Using AWT Components

The document discusses Java AWT components and how to create a GUI using them. It describes key classes like Frame, Panel, Checkbox, Choice, Label and Scrollbar. It provides examples of creating each component and adding them to containers like Frame and Panel. Code snippets and output screenshots are included to demonstrate creating a GUI with these AWT components.

Uploaded by

moheda214
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Academic Year 2024

Class: BIT28

Chapter 01

Creating GUI Using AWT Components

Faculty of Computing
SIMAD University

JAVA Programming SIMAD University 1


Overview

Java AWT (Abstract Window Toolkit) is an API to develop Graphical


User Interface (GUI) or windows-based applications in Java.
Java AWT contains numerous classes and methods that allow you to
creates windows and simple controls. Also the AWT classes are
contained in the java.awt package. It is one of Java's largest
packages.
Java AWT components are platform-dependent i.e. components are
displayed according to the view of operating system. AWT is heavy
weight i.e. its components are using the resources of underlying
operating system (OS).
The java.awt packages provides classes for AWT API such
as TextField, Label, TextArea, RadioButton, Checkbox, Choice etc.

JAVA Programming SIMAD University 2


Java AWT Hierarchy
At the top of the AWT hierarchy is the component class .
Component is the abstract class that encapsulate all of the
attribute of visual component
 All user interface elements are displayed on the screen that
interacts with the user are subclass of component.
 It defines hundred public methods that are responsible for
managing events such as mouse and keyboard input, positioning
and sizing the window.

Figure 1.1 AWT Hierarchy


JAVA Programming SIMAD University 3
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.

Container It is basically a screen where the components are


placed at their specific locations. Thus it contains and
controls the layout of components.it means it is responsible
for laying out (Positioning) any component that it contains.

JAVA Programming SIMAD University 4


Panel class is the subclass of container, that doesn't contain
title bar, border or menu bar. It is generic container for
holding the components.
Panel class can have other components like button, text field
etc. An instance of Panel class creates a container, in which
we can add components.
Window class is subclass of container that have no borders
and menu bars. You must its own subclass called Frame,
dialog or another window for creating a window. We need to
create an instance of Window class to create this container.

JAVA Programming SIMAD University 5


Frame is a subclass of Window, 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.
As the mentioned, it creates a standard-style, top level
window that has all features normally associated with an
application window such as close box and title.
Here are two frame constructor
 Frame(): creates a standard window that doesn’t contain a title.
 Frame(String title): creates window with the title
specified by title.
JAVA Programming SIMAD University 6
Frame class
One constructor takes a single argument(the title to be
displayed at the top of the frame).
Frame f= new Frame(Title of the frame);
Although the frame object now exists, but it’s not visible on
the screen. Before making the frame visible, the method
should be called to set the size of the frame.
The setSize method sets the width and height of the frame.
f.setSize(width, height);
The setVisible method controls whether is not frame
currant visible on the screen.

JAVA Programming SIMAD University 7


Calling setVisible width true as the argument makes
frame visible:
f. setVisible (true);
Calling setVisible width False as the argument makes frame
disappear from the screen:
f. setVisible (false);
We create frame object called FrameTest and display on the
screen.
The Following program illustrates three key steps:
 Using frame constructor to create a frame.
 Setting the size of frame.
 Displaying the frame on the screen.
JAVA Programming SIMAD University 8
JAVA Programming SIMAD University 9
Figure 1.2 Frame output

Frame created by FrameTest Program


As with the other AWT component the appearance of the
frame depends on the platform.

JAVA Programming SIMAD University 10


Java AWT Control

Java AWT controls are the controls that are used to design
graphical user interfaces or web applications. To make an
effective GUI,
Java provides java.awt package that supports various
AWT controls like Label, Button, CheckBox, CheckBox
Group, List, Text Field, Text Area, Choice, Scrollbar etc.
These controls are subclasses of Component.

JAVA Programming SIMAD University 11


Adding Controls

To add a component to a frame( or any kind of container) the


add() method is used.
First f create an instance of the desired control and then add
it to a window by calling add().
Now we adding components to a frame.
f. add(“Component”);

JAVA Programming SIMAD University 12


JAVA Programming SIMAD University 13
Panels

The Panel is a simplest container class. It provides space in


which an application can attach any other component. It
inherits the Container class. And it doesn’t have a title bar.
Panel can used to create group of components that it treated as
a single component.
Once a panel has been created, components are added to it by
calling a add() method.
The panel its self will need to be added to a frame or other
container.

JAVA Programming SIMAD University 14


JAVA Programming SIMAD University 15
The output will look like the following

Figure 1.3 Panel output

JAVA Programming SIMAD University 16


Checkbox
A checkbox is a small box that the user can check by clicking
with the mouse.
A checkbox. It is used to turn an option on (true) or off (false).
Clicking on a Checkbox changes its state from "on" to "off" or
from "off" to "on".
Clicking on the box causes a check mark to appear. Clicking
second time removes a check mark from the box.

Figure1.4 Checkbox group


JAVA Programming SIMAD University 17
JAVA Programming SIMAD University 18
Figure 1.5 Checkbox output

JAVA Programming SIMAD University 19


Checkbox group
Checkbox group is a collection of checkboxes in which only
one checkbox can checked at a time.

A checkboxes that are related in this


way are often referred to as radio button.
The first step in creating a group of checkboxes is to create a
checkbox group object.
CheckboxGroup childGroup = new CheckboxGroup();

JAVA Programming SIMAD University 20


JAVA Programming SIMAD University 21
Figure 1.6 Checkbox group output

Only one checkbox can checked at a time.


JAVA Programming SIMAD University 22
Choice menu

Choice class is used to show popup menu of choices. Choice


selected by user is shown on the top of a menu. It inherits
Component class.
When the user presses on the arrow button with the mouse the
full list of the choices will pops up.

Figure 1.7 Choice menu


JAVA Programming SIMAD University 23
JAVA Programming SIMAD University 24
Figure 1.8 Choice output

JAVA Programming SIMAD University 25


Label
Label is a component for placing text in a container. It is used
to display a single line of read only text. The text can be
changed by a programmer but a user cannot edit it directly.
Label are often placed next to the other components to indicate
their meaning function.

It is called a passive control as it does not create any event


when it is accessed. To create a label, we need to create the
object of Label class.
Label label1 = new Label("First Name");

JAVA Programming SIMAD University 26


JAVA Programming SIMAD University 27
Figure 1.9 Label output

JAVA Programming SIMAD University 28


Scrollbar

The object of Scrollbar class is used to add horizontal and


vertical scrollbar. Scrollbar is a GUI component allows us to
see invisible number of rows and columns.
It can be added to top-level container like Frame or a
component like Panel. The Scrollbar class extends
the Component class.

JAVA Programming SIMAD University 29


JAVA Programming SIMAD University 30
Chapter 01 Assignments

Individual Assignment
Assignment One
Difference between TextField and TextArea in Java AWT, (concept & Practice)

JAVA Programming SIMAD University 31

You might also like