Java Reference
In-Depth Information
}
Of course, because
SystemColor
is a subclass of the
Color
class, it inherits all the members of that class,
including all the color constants that you saw in the previous section.
Creating Cursors
An object of the
java.awt.Cursor
class encapsulates a bitmap representation of the mouse cursor. The
Cursor
class contains a range of
final static
constants that specify standard cursor types. You use these
to select or create a particular cursor. The standard cursor types are the following:
DEFAULT_CURSOR N_RESIZE_CURSORNE_RESIZE_CURSOR
CROSSHAIR_CURSORS_RESIZE_CURSORNW_RESIZE_CURSOR
WAIT_CURSOR
E_RESIZE_CURSORSE_RESIZE_CURSOR
TEXT_CURSOR
W_RESIZE_CURSORSW_RESIZE_CURSOR
HAND_CURSOR
MOVE_CURSOR
The resize cursors are the ones that you see when you resize a window by dragging its boundaries. Note
that these are not like the
Color
constants, which are
Color
objects — these constants are of type
int
, not
type
Cursor
, and are intended to be used as arguments to a
Cursor
constructor. To create a
Cursor
object
representing a text cursor you could write:
Cursor myCursor = new Cursor(Cursor.TEXT_CURSOR);
Alternatively, you can retrieve a predefined cursor using a
static
class method:
Cursor myCursor = Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR);
This method is particularly useful when you don't want to store the
Cursor
object, but just want to pass
it to a method, such as
setCursor()
for a
Component
object.
If you want to see what the standard cursors look like, you could add a cursor to an application window,
along with a pink background.
TRY IT OUT: Color and Cursors
You can change the background color of the content pane for the application window and try out a dif-
ferent cursor:
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Cursor;