Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 14

Introduction to GUI

Programming
Chapter Topics
 Event-driven programming
 Graphical user interfaces
 The three parts of a GUI
 GUI components
 Containers
 The Abstract Windowing Toolkit (AWT)
 How the AWT relates to Swing
 Swing class hierarchy
Swing and the AWT
A graphical user interface consists
of graphical components, such as:
 Windows
 Buttons
 Menus
 Text fields
Swing and the AWT
Swing components are based on the fundamental classes of
the Application Windowing Toolket (AWT).
The AWT also contains some older components which are no
longer used.
Swing components come with recent java Java releases.
Swing integrates smoothly with the Java Foundation Classes.
The Java Foundation Classes provide many classes needed for
modern software development.
Events
A GUI application program shows the user a graphical interface
containing several graphical components. The user controls the
application by interacting with the graphical components, doing such
things as:
 Clicking on a button to choose a program option.
 Making a choice from a menu.
 Entering text in a text field.
 Dragging a scroll bar.
An action such as clicking on a button is called an event. In reading
these notes you have been generating events by using the graphical
interface of your Web browser.
Events-driven Programming
 When you perform an action on a graphical component you generate
an event. In event-driven programming the program responds to
these events. The order of the events is determined by the user, not the
program.
 For example, you can generate an event by clicking on this button:
Generate an Event, Or you could click on this button: Generate
another Event    Click on the buttons a few more times in any order.
The program (your Web browser, in this case) is event-driven and
responds to the events in whatever order you generate them.
Responding to Events
Three Parts of a GUI Program
A GUI program has three parts:

A. Graphical Components that make up the Graphical User


Interface.
B. Listener methods that receive the events and respond to
them.
C. Application methods that do useful work for the user.
 Consider a radio and its user interface. Match each part of the radio
with its analogous part in a GUI application.
Real-world Interfaces
 Non-software devices usually have user interfaces, too.
They have:
 Components that make up the user interface.
 Knobs, buttons, switches, dials, lights, levers, pedals...
 Parts that are connected to the interface components.
 These "translate" user actions into something the rest
of the device can act on.
 Parts of the device that does the actual work.
 Motors, gears, axles, tubes, wires, valves...
Container Classes
 A GUI program consists of a collection of graphical components that
are all placed inside one or more windows. The components
are contained by the window. A container is an object that can hold
other GUI components. Visually, a container is an area of the screen
and the objects it contains are shown as smaller areas within it.
 In Java terminology, a window (such as the browser window you
are looking at) is a container. Buttons, sliders, icons and other GUI
components are always put in container.
Java Classes
AWT Hierarchy
 The Java AWT (Abstract Windowing Toolkit)
contains the fundamental classes used for
constructing GUIs. The abstract Component class is
the base class for the AWT. Many AWT classes are
derived from it. These are the old AWT components
that are no longer in use.
 Some of the AWT classes derived
from Component are Button, Canvas, and Container.
AWT Hierarchy
The diagram shows how the
classes of the AWT and Swing
fit together. Look it over to get
an overview. Refer back to it
occasionally as you read these
notes.

The JComponent class is derived
from Container and is one of
the base classes of Swing.
The JFrame class is derived from
the AWT Frame class. It is usually
the main container for a GUI
application.
The JApplet class is derived from
the AWT Applet class and is used
for modern applets.

You might also like