Intro to GUI and Event Handling

Download Report

Transcript Intro to GUI and Event Handling

Chapter 7
Event-Driven Programming and Basic
GUI Objects
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Introduction
This chapter covers the graphical user
interface (GUI). In Java, GUI-based
programs are implemented by using
classes from the javax.swing and
java.awt packages.
The Swing classes provide greater
compatibility across different operating
systems. They are fully implemented
in Java, and behave the same on
different operating systems.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Fig. 7.1
Various GUI objects from the
javax.swing package.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Introduction
AWT classes are implemented by using
the native GUI objects.
Swing classes support many new
functionalities not supported by AWT
counterparts.
Do not mix the counterparts in the same
program because of their differences
in implementation.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Introduction
To build an effective GUI using objects
from the Swing and AWT packages,
we must learn a new style of program
control called event-driven
programming.
An event occurs when the user interacts
with a GUI object.
In event-driven programs, we program
objects to respond to these events by
defining event-handling methods.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
7.1 Creating a Subclass of JFrame
To create a customized user interface,
we often define a subclass of the
JFrame class.
The JFrame class contains rudimentary
functionalities to support features
found in any frame window.
http://java.sun.com/j2se/1.5.0/docs/api/i
ndex.html
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Fig. 7.4
How an instance of
Ch7JFrameSubclass1 will appear on
the screen.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
7.1 Creating a Subclass of JFrame
We will also add the following default
characteristics:
• The title is set to My First Subclass.
• The program terminates when the close
box is clicked.
• The size of the frame is 300 pixels wide by
200 pixels high.
• The frame is positioned at screen
coordinate (150, 250).
These properties are set inside the
default constructor.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
7.1 Creating a Subclass of JFrame
Next we will define another subclass
called Ch7JFrameSubclass2, which
will have a white background.
We will define this class as an
instantiable main class so we don’t
have to define a separate main class.
http://java.sun.com/j2se/1.5.0/docs/api/i
ndex.html
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
7.1 Creating a Subclass of JFrame
To make the background white, we must
access the frame’s content pane, the
area of the frame excluding the title
and menu bars and the border.
We access the content pane by calling
the frame’s getContentPane method.
We change the background color by
calling the content pane’s
setBackground method.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
7.1 Creating a Subclass of JFrame
Load Ch7FrameSubclass1.java
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
7.2 Placing Buttons on the
Content Pane of a Frame
There are two approaches to placing GUI
objects on a frame’s content pane.
One approach uses a layout manager, an
object that controls the placement of the GUI
objects. (will use later in course)
The other approach uses absolute positioning
to explicitly specify the position and size of
GUI objects on the content pane.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Fig. 7.7
The process of creating a button and
placing it on a frame.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
7.2 Placing Buttons on the
Content Pane of a Frame
To use absolute positioning, set the
layout manager of a frame’s content
pane to none by passing null to the
setLayout method.
contentPane.setLayout(null);
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
7.2 Placing Buttons on the
Content Pane of a Frame
We then place two buttons at the position
and size we want by calling the button’s
setBounds method:
okButton.setBounds( 75, 125, 80, 30);
The first two arguments specify the
button’s position.
The last two arguments specify the width
and height of the button.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
7.2 Placing Buttons on the
Content Pane of a Frame
To make a button appear on the frame,
add it to the content pane by calling
the add method.
contentPane.add(okButton);
Load code: Ch7ButtonFrame.Java
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
7.3 Handling Button Events
An action involving a GUI object, such
as clicking a button, is called an event.
The mechanism to process events is
called event handling.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
7.3 Handling Button Events
The event-handling model of Java is
based on the concept known as the
delegation-based event model.
With this model, event handling is
implemented by two types of objects:
• event source objects
• event listener objects.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
7.3 Handling Button Events
An event source object is a GUI object
where an event occurs. An event
source generates events.
An event listener object is an object that
includes a method that gets executed
in response to the generated events.
When an event is generated, the
system notifies the relevant event
listener objects.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
7.3 Handling Button Events
There are many different kinds of
events, but the most common one is
an action event.
For the generated events to be
processed, we must associate, or
register, event listeners to the event
sources.
If event sources have no registered
listeners, the generated events are
ignored.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
7.3 Handling Button Events
An object that can be registered as an
action listener must be an instance of
a class that is declared specifically for
the purpose. We call such classes
action listener classes.
To associate an action listener to an
action event source, we call the event
source’s addActionListener method
with the action listener as its
argument.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
7.3 Handling Button Events
A single listener can be associated to
multiple event sources.
Likewise, multiple listeners can be
associated to a single event source.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
7.3 Handling Button Events
When an event source generates an
event, the system checks for matching
registered listeners.
If there is no matching listener, the
event is ignored.
If there is a matching listener, the
system notifies the listener by calling
the listener’s corresponding method.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
7.3 Handling Button Events
In the case of action events, the method
called is actionPerformed.
To ensure the programmer includes the
necessary actionPerformed method in
the action listener class, the class must
be defined in a specific way.
import java.awt.event.*;
class ButtonHandler implements
ActionListener {
...
}
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
7.3 Handling Button Events
A class that implements a Java interface
must provide the method body to all
the abstract methods defined in the
interface.
By requiring an object we pass as an
argument to the addActionListener
method to be an instance of a class
that implements the ActionListener
interface, the system ensures that this
object will include the necessary
actionPerformed method.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
7.3 Handling Button Events
To change the title of the frame, depending
on which button is clicked, we use the
actionPerformed method. The method
model is:
public void actionPerformed(ActionEvent
evt){
String buttonText
= get the text of the event source;
JFrame frame
= the frame that contains this event
source;
frame.setTitle(“You clicked “ +
buttonText);
}
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
7.3 Handling Button Events
View source code.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
7.3 Handling Button Events
The second way is via the getSource
method of the action event object evt.:
JButton clickedButton =
(JButton) evt.getSource();
String buttonText = clickedButton.getText();
Note that the object returned by the
getSource method may be an instance
of any class, so we type cast the returned
object to a proper class in order to use
the desired method.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
7.3 Handling Button Events
To find the frame that contains the event
source, we
• get the root pane to which the event
source belongs, then
• get the frame that contains this root pane.
JRootPane rootPane =
clickedButton.getRootPane();
Frame frame = (JFrame)
rootPane.getParent();
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.