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

JavaGui

Uploaded by

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

JavaGui

Uploaded by

ranganadh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Graphical User Interface

Graphical User Interface (GUI) offers user interaction via some graphical components. For
example our underlying Operating System also offers GUI via window,frame,Panel, Button,
Textfield, TextArea, Listbox, Combobox, Label, Checkbox etc. These all are known as
components. Using these components we can create an interactive user interface for an
application.

GUI provides result to end user in response to raised events.GUI is entirely based events. For
example clicking over a button, closing a window, opening a window, typing something in a
textarea etc. These activities are known as events.GUI makes it easier for the end user to use an
application. It also makes them interesting.

Basic Terminologies

Term Description

Component Component is an object having a graphical representation that can be


displayed on the screen and that can interact with the user. For examples
buttons, checkboxes, list and scrollbars of a graphical user interface.

Container Container object is a component that can contain other


components.Components added to a container are tracked in a list. The
order of the list will define the components' front-to-back stacking order
within the container. If no index is specified when adding a component to
a container, it will be added to the end of the list.

Panel Panel provides space in which an application can attach any other
components, including other panels.

Window Window is a rectangular area which is displayed on the screen. In different


window we can execute different program and display different data.
Window provide us with multitasking environment. A window must have
either a frame, dialog, or another window defined as its owner when it's
constructed.

Frame A Frame is a top-level window with a title and a border. The size of the
frame includes any area designated for the border. Frame
encapsulates window. It and has a title bar, menu bar, borders, and
resizing corners.

Canvas Canvas component represents a blank rectangular area of the screen onto
which the application can draw. Application can also trap input events
from the use from that blank area of Canvas component

Examples of GUI based Applications


Following are some of the examples for GUI based applications.

 Automated Teller Machine (ATM)


 Airline Ticketing System
 Information Kiosks at railway stations
 Mobile Applications
 Navigation Systems
Advantages of GUI over CUI
 GUI provides graphical icons to interact while the CUI (Character User Interface) offers
the simple text-based interfaces.
 GUI makes the application more entertaining and interesting on the other hand CUI
does not.
 GUI offers click and execute environment while in CUI every time we have to enter the
command for a task.
 New user can easily interact with graphical user interface by the visual indicators but it
is difficult in Character user interface.
AWT Controls (Abstract Window ToolKit)
Every user interface considers the following three main aspects:

 UI elements :Thes are the core visual elements the user eventually sees and interacts
with. GWT provides a huge list of widely used and common elements varying from basic
to complex which we will cover in this tutorial.
 Layouts: They define how UI elements should be organized on the screen and provide a
final look and feel to the GUI (Graphical User Interface).
 Behavior: These are events which occur when the user interacts with UI elements
AWT UI Elements:
Following is the list of commonly used controls while designed GUI using AWT.
Sr. Control & Description
No.

1 Label

A Label object is a component for placing text in a container.

2 Button

This class creates a labeled button.

3 Check Box

A check box is a graphical component that can be in either an on (true)


or off (false) state.

4 Check Box Group

The CheckboxGroup class is used to group the set of checkbox.

5 List

The List component presents the user with a scrolling list of text items.

6 Text Field

A TextField object is a text component that allows for the editing of a single line
of text.

7 Text Area

A TextArea object is a text component that allows for the editing of a multiple
lines of text.

8 Choice

A Choice control is used to show pop up menu of choices. Selected choice is


shown on the top of the menu.

9 Canvas

A Canvas control represents a rectangular area where application can draw


something or can receive inputs created by user.

10 Image

An Image control is superclass for all image classes representing graphical


images.

11 Scroll Bar

A Scrollbar control represents a scroll bar component in order to enable user to


select from range of values.

12 Dialog

A Dialog control represents a top-level window with a title and a border used to
take some form of input from the user.

13 File Dialog

A FileDialog control represents a dialog window from which the user can select a
file.
Event Handling
What is an Event?
Change in the state of an object is known as event i.e. event describes the change in state of
source. Events are generated as result of user interaction with the graphical user interface
components. For example, clicking on a button, moving the mouse, entering a character
through keyboard,selecting an item from list, scrolling the page are the activities that causes an
event to happen.
Types of Event
The events can be broadly classified into two categories:
 Foreground Events - Those events which require the direct interaction of user.They are
generated as consequences of a person interacting with the graphical components in
Graphical User Interface. For example, clicking on a button, moving the mouse, entering
a character through keyboard,selecting an item from list, scrolling the page etc.
 Background Events - Those events that require the interaction of end user are known as
background events. Operating system interrupts, hardware or software failure, timer
expires, an operation completion are the example of background events.

What is Event Handling?


Event Handling is the mechanism that controls the event and decides what should happen if an
event occurs. This mechanism have the code which is known as event handler that is executed
when an event occurs.
Steps involved in event handling
 The User clicks the button and the event is generated.
 Now the object of concerned event class is created automatically and information about
the source and the event get populated with in same object.
 Event object is forwarded to the method of registered listener class.
 the method is now get executed and returns.

Points to remember about listener


 In order to design a listener class we have to develop some listener interfaces.These
Listener interfaces forecast some public abstract callback methods which must be
implemented by the listener class.
 If you do not implement the any if the predefined interfaces then your class can not act
as a listener class for a source object.

APPLETS
Applets are GUI based applications. Applets are created by extending a class
called "Applet".The "Applet" class is available in the "java.applet" package.

Life cycle of an Applet :-


The Life cycle of an "Applet" comprises of 4 methods.
1. init()
2. start()
3. stop()
4. destroy()

1. init()
This is the first method to be executed, when the "Applet" is first run. Mostly
used for initialization of variables and objects.

2. start()
This is the second method to be executed after the init(). This constitutes the
main code of your "Applet".

3. stop()
This method is executed when the "Applet" loses focus.

4. destroy()
This method is executed when the "Applet" is totally shut down.

// Prg on working with Applets (GUI)


import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Applet_Eg1 extends Applet implements ActionListener
{
Button b1,b2,b3;
Font f1=new Font("verdana",Font.BOLD,14);

public void init()


{
setLayout(new FlowLayout(FlowLayout.RIGHT));
b1=new Button("Green");
b2=new Button("Red");
b3=new Button("Yellow");
b1.setFont(f1);b2.setFont(f1);b3.setFont(f1);
add(b1);add(b2);add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}

public void actionPerformed(ActionEvent e)


{
if (e.getSource()==b1)
setBackground(Color.green);
else
if (e.getSource()==b2)
setBackground(Color.red);
else
if (e.getSource()==b3)
setBackground(Color.yellow);
}
}

Output

You might also like