Download Lab 4.1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Faculty of Engineering & IT
Software Engineering Department
Computer Science [3]
Java Programming II - Laboratory Course
Lab 4:
Common GUI Event Types and Listener Interfaces
Layout Mangers
Eng.Omar Al-Nahal
WWW.PALINFONET.COM
Event-Driven Programming

Programs with GUIs must respond to events, generated by
GUI components, that indicate that specific actions have
occurred.

A special category of classes, called listeners, wait for events
to occur.

Therefore, a GUI program is composed of:
• the code that presents the GUI to the user
• the listeners that wait for events to occur
• the specific code that is executed when events occur.
2
Event-Driven Programming
There is a listener interface defined for each event type
 Each listener interface contains the abstract methods
required to respond to specific events

A listener class implements a particular listener
interface
 Listeners are "added" to a particular GUI component
 When a component generates an event, the method
corresponding to that event is executed in the listener

3
The GUI Program Model
Listeners
Handle events
Add listeners
GUI
Event effects
Programspecific
4
Common GUI Event Types and Listener Interfaces
For each event-object type, there is typically a corresponding eventlistener interface. An event listener for a GUI event is an object of a
class that implements one or more of the event-listener interfaces
from packages java.awt.event & javax.swing.event. Many of the
event-listener types are common to both Swing and AWT
components. Such types are declared in package java.awt.event
To Make an Interactive GUI Program
To make an interactive GUI program, you need:
Components
- buttons, windows, menus, etc.
Events
- mouse clicked, window closed, button clicked, etc.
Event listeners (interfaces) and event handlers (methods)
- listen for events to be trigged, and then perform actions to handle them.
Event-Handling Model (1)
some event classes in package java.awt.event
ActionEvent
ContainerEvent
AdjustmentEvent
FocusEvent
ComponentEvent
InputEvent
ItemEvent
PaintEvent
TextEvent
WindowEvent
CLASS
KeyEvent
MouseEvent
Event-Handling Model (2)

Three parts of the event-handling
mechanism
•
•
•

event source: the GUI component
with which the user interacts
event object: encapsulated
information about the occurred event
event listener: an object which is
notified by the event source when an
event occurs, and provides responds
to the event
The programmer must perform two
tasks to process a GUI event
1.
register an event listener
An object of a class that implements one or more
of the event-listener interfaces from
packages java.awt.event and
javax.swing.event
2.
implement an event handling method
Event-listener interface of
package java.awt.event
Lists

A list, in the Java GUI sense, is used to display a list
selectable strings.

A list component can contain any number of strings and
can be instantiated to allow multiple selections within it.

The size of the list is specified by the number of visible rows
or strings within it.

A scrollbar will automatically appear on the right side of a
list if the number of items exceed the visible area.

A list is defined by the List class
See List.java
8
Scrollbars

A scrollbar is a slider that indicates a relative position or
quantity.

They are automatic on text areas and list components, but
can be used independently.

The position of the slider in the range corresponds to a
particular numeric value in a range associated with the
scrollbar.

A scrollbar is defined by the Scrollbar class
9
Layout Managers

There are five predefined layout managers in the
java.awt package:
•
•
•
•
•
Flow layout
Border layout
Grid layout
Card layout
Grid bag layout

Each container has a particular layout manager associated
with it by default.

A programmer can also create custom layout managers
10
Flow Layout

Components are placed in a row from left to right in the
order in which they are added.

A new row is started when no more components can fit in
the current row.

The components are centered in each row by default.

The programmer can specify the size of both the vertical
and horizontal gaps between the components.

Flow layout is the default layout for panels and applets
See Flow.java
11
Grid Layout




Components are placed in a grid with a user-specified
number of columns and rows
Each component occupies exactly one grid cell
Grid cells are filled left to right and top to bottom
All cells in the grid are the same size See Grid.java
Border Layout

Defines five locations each of which a component or
components can be added
• North, South, East, West, and Center See Border.java
12
Border Layout
North
West
Center
East
South
Sample:
Color , ShowColor , Font , Menu212 ,
Painter , PanelDemo , LinesRectsOvals ,
LookAndFeelDemo , BoxLayoutDemo , JDesktopPane
13
For More Information
- Supplemental Reading
http://java.sun.com/docs/books/tutorial/ui/features/components.html
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JComponent.html
http://java.sun.com/developer/onlineTraining/new2java/divelog/part1/
http://java.sun.com/developer/onlineTraining/new2java/divelog/part2/index.jsp
http://java.sun.com/developer/onlineTraining/new2java/divelog/part3/
http://java.sun.com/developer/onlineTraining/new2java/divelog/part4/
http://java.sun.com/developer/onlineTraining/new2java/divelog/part5
WWW.PALINFONET.COM