Java Reference
In-Depth Information
10.6 Syntax
A new timer is created. The
o
object must
belong to a class that implements the interface
ActionListener
. The statement
o.actionPerformed()
will be executed every 20 milliseconds.
⇒
Timer t = new Timer(20, o);
t.start();
⇒
Starts the timer.
t.stop();
⇒
Stops the timer.
C1.C2 o = new C1.C2();
⇒
Creates a new
o
object that belongs to the static nested
C2
class, which is inside the
C1
class.
o.new C2()
⇒
Creates a new object that belongs to the inner
C2
class inside the
C1
class. The
o
object must belong to the
C1
class and is the outer object for the newly
created object.
⇒
C1.this.m();
Calls the
m
method that belongs to the outer
C1
class. The call is
only valid from an inner class (i.e., non-static nested class).
o = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
...
}}
;
⇒
Creates a new object that belongs to an anonymous
X
class. The
X
class must
implement the interface
ActionListener
and therefore override the
actionPerformed
method. The body of the
X
class is specified between the inner pair of braces.
panel.addKeyListener(new KeyAdapter()
Creates and registers a new
key listener. The
KeyAdapter
class implements the interface
KeyListener
with empty
body for the
keyPressed
,
keyReleased
,and
keyTyped
methods.
{
...
}
);
⇒
panel.addMouseListener(new MouseAdapter()
{
Creates and registers a
new mouse listener. The
MouseAdapter
class implements the
MouseListener
interface
with empty body for the
mouseClicked
,
mouseEntered
,
mouseExited
,
mousePressed
,
and
mouseReleased
methods.
...
}
);
⇒
Creates
and registers a new mouse motion listener. The
MouseMotionAdapter
class imple-
ments the
MouseMotionListener
interface with empty body for the
mouseMoved
and
mouseDragged
methods.
{
...
}
⇒
panel.addMouseMotionListener(new MouseMotionAdapter()
);
e.getX()
⇒
Gets the
x
coordinate. Can be called inside any mouse listener or mouse
motion listener method. The coordinate is relative to the top left corner of the event
source (e.g., window or panel).
Gets the
y
coordinate. Can be called inside any mouse listener or mouse
motion listener method. The coordinate is relative to the top left corner of the event
source (window or panel).
⇒
e.getY()
e.getPoint()
⇒
Returns the point of where the cursor is currently. Can be called in-
side any mouse listener or mouse motion listener method. The coordinates are relative
to the top left corner of the event source (e.g., window or panel).
e.getButton()
⇒
Returns the mouse button on which the event
e
occurred (1 is
the left button, 2 is the middle, 3 is the right button). Can be called in the following
mouse listener methods:
mouseClicked
,
mousePressed
,and
mouseReleased
.