Java Reference
In-Depth Information
duced the layout manager FlowLayout . Here we use the default layout of a JFrame 's content
pane— BorderLayout , which arranges component NORTH , SOUTH , EAST , WEST and CENTER
regions. NORTH corresponds to the container's top. This example uses the CENTER and SOUTH
regions. Line 25 uses a two-argument version of method add to place mousePanel in the
CENTER region. The BorderLayout automatically sizes the component in the CENTER to use
all the space in the JFrame that is not occupied by components in the other regions.
Section 12.18.2 discusses BorderLayout in more detail.
Lines 27-28 in the constructor declare JLabel statusBar and attach it to the
JFrame 's SOUTH region. This JLabel occupies the width of the JFrame . The region's height
is determined by the JLabel .
Line 31 creates an instance of inner class MouseHandler (lines 36-97) called handler
that responds to mouse events. Lines 32-33 register handler as the listener for mouse-
Panel 's mouse events. Methods addMouseListener and addMouseMotionListener are
inherited indirectly from class Component and can be used to register MouseListener s and
MouseMotionListener s, respectively. A MouseHandler object is a MouseListener and is a
MouseMotionListener because the class implements both interfaces. We chose to imple-
ment both interfaces here to demonstrate a class that implements more than one interface,
but we could have implemented interface MouseInputListener instead.
When the mouse enters and exits mousePanel 's area, methods mouseEntered (lines
65-71) and mouseExited (lines 74-79) are called, respectively. Method mouseEntered
displays a message in the statusBar indicating that the mouse entered the JPanel and
changes the background color to green. Method mouseExited displays a message in the
statusBar indicating that the mouse is outside the JPanel (see the first sample output
window) and changes the background color to white.
The other five events display a string in the statusBar that includes the event and the
coordinates at which it occurred. MouseEvent methods getX and getY return the x- and y -
coordinates, respectively, of the mouse at the time the event occurred.
12.15 Adapter Classes
Many event-listener interfaces, such as MouseListener and MouseMotionListener , con-
tain multiple methods. It's not always desirable to declare every method in an event-listen-
er interface. For instance, an application may need only the mouseClicked handler from
MouseListener or the mouseDragged handler from MouseMotionListener . Interface Win-
dowListener specifies seven window event-handling methods. For many of the listener in-
terfaces that have multiple methods, packages java.awt.event and javax.swing.event
provide event-listener adapter classes. An adapter class implements an interface and pro-
vides a default implementation (with an empty method body) of each method in the in-
terface. Figure 12.30 shows several java.awt.event adapter classes and the interfaces they
implement. You can extend an adapter class to inherit the default implementation of every
method and subsequently override only the method(s) you need for event handling.
Software Engineering Observation 12.6
When a class implements an interface, the class has an is-a relationship with that
interface. All direct and indirect subclasses of that class inherit this interface. Thus, an
object of a class that extends an event-adapter class is an object of the corresponding event-
listener type (e.g., an object of a subclass of MouseAdapter is a MouseListener ).
 
 
Search WWH ::




Custom Search