Java Reference
In-Depth Information
The user interface is created in the constructor (lines 18-44). The button is the source of the
event. A listener is created and registered with the button (line 43).
The ButtonListener class (lines 47-66) implements the actionPerformed method.
When the button is clicked, the actionPerformed method is invoked to get the interest rate
(line 51), number of years (line 53), and loan amount (line 54). Invoking
jtfAnnualInterestRate.getText() returns the string text in the
jtfAnnualInterestRate text field. The Loan class is used for computing the loan payments.
This class was introduced in Listing 10.2, Loan.java. Invoking loan.getMonthlyPayment()
returns the monthly payment for the loan (line 62). The String.format method, introduced in
Section 9.2.11, is used to format a number into a desirable format and returns it as a string (lines
61, 63). Invoking the setText method on a text field sets a string value in the text field (line 61).
16.8 Mouse Events
A mouse event is fired whenever a mouse button is pressed, released, or clicked, the
mouse is moved, or the mouse is dragged onto a component.
Key
Point
The MouseEvent object captures the event, such as the number of clicks associated with it,
the location (the x - and y -coordinates) of the mouse, or which button was pressed, as shown in
Figure 16.12.
java.awt.event.InputEvent
+getWhen(): long
Returns the timestamp when this event occurred.
+isAltDown(): boolean
+isControlDown(): boolean
+isMetaDown(): boolean
Returns true if the Alt key is pressed on this event.
Returns true if the Control key is pressed on this event.
Returns true if the Meta mouse button is pressed on this event.
+isShiftDown(): boolean
Returns true if the Shift key is pressed on this event.
java.awt.event.MouseEvent
+getButton(): int
Indicates which mouse button has been clicked.
+getClickCount(): int
+getPoint(): java.awt.Point
+getX(): int
Returns the number of mouse clicks associated with this event.
Returns a Point object containing the x - and y -coordinates.
Returns the x -coordinate of the mouse point.
+getY(): int
Returns the y -coordinate of the mouse point.
F IGURE 16.12
The MouseEvent class encapsulates information for mouse events.
Since the MouseEvent class inherits InputEvent , you can use the methods defined in
the InputEvent class on a MouseEvent object. For example, the isControlDown()
method detects whether the CTRL key was pressed when a MouseEvent is fired.
Three int constants— BUTTON1 , BUTTON2 , and BUTTON3 —are defined in MouseEvent
to indicate the left, middle, and right mouse buttons. You can use the getButton() method
to detect which button is pressed. For example, getButton() == MouseEvent.BUTTON3
indicates that the right button was pressed.
The java.awt.Point class represents a point on a component. The class contains two
public variables, x and y , for coordinates. To create a Point , use the following constructor:
detect mouse buttons
Point class
Point( int x, int y)
 
 
Search WWH ::




Custom Search