• Tidak ada hasil yang ditemukan

Programming in Java

N/A
N/A
Protected

Academic year: 2024

Membagikan "Programming in Java"

Copied!
7
0
0

Teks penuh

(1)

Programming in Java

CBCS BHCS03

By Parul Chachra

(2)

Chapter 24

Event Handling

Reference: Book prescribed as per

the syllabus

(3)

KeyEvent Class

•  A KeyEvent is generated when keyboard input occurs.

•  There are three types of key events, which are idenJfied by these integer constants:

–  KEY_PRESSED: key pressed –  KEY_RELEASED: key released

–  KEY_TYPED: when a character is generated. Remember, not all keypresses result in characters. For example, pressing SHIFT does not generate a character.

•  There are many other integer constants that are defined by KeyEvent. For example, VK_0 through VK_9 and VK_A

through VK_Z define the ASCII equivalents of the numbers and lePers. Here are some others:

–  VK_ALT

–  VK_DOWN

–  VK_RIGHT

–  VK_CANCEL

–  VK_ENTER

(4)

KeyEvent Class

•  The VK constants specify virtual key codes and are independent of any modifiers, such as control, shiY, or alt.

•  KeyEvent is a subclass of InputEvent. Here is one of its constructors:

KeyEvent(Component src, int type, long when, int modifiers, int code, char ch)

•  Here, src is a reference to the component that generated the event. The type of the event is specified by type. The system Jme at which the key was pressed is passed in when. The modifiers argument indicates which modifiers were pressed when this key event occurred. The virtual key

code, such as VK_UP, VK_A, and so forth, is passed in code. The character equivalent (if one exists) is passed in ch. If no valid character exists, then ch contains CHAR_UNDEFINED. For KEY_TYPED events, code will contain VK_UNDEFINED.

•  The KeyEvent class defines several methods, but the most commonly used ones are:

–  getKeyChar( ), which returns the character that was entered. General form is:

char getKeyChar( )

–  getKeyCode( ), which returns the key code. General form is:

int getKeyCode( )

•  If no valid character is available, then getKeyChar( ) returns

CHAR_UNDEFINED. When a KEY_TYPED event occurs, getKeyCode( ) returns VK_UNDEFINED.

(5)

MouseEvent Class

•  MouseEvent class defines the following

integer constants that can be used to eight

events defined by the class:

(6)

MouseEvent Class

•  MouseEvent is a subclass of InputEvent

•  Here is one of its constructors:

MouseEvent(Component src, int type, long when, int

modifiers, int x, int y, int clicks, boolean triggersPopup)

•  Here, src is a reference to the component that generated the event. The type of the event is specified by type. The system Jme at which the mouse event occurred is passed in when. The modifiers argument indicates which modifiers were pressed when a mouse event occurred. The coordinates of the mouse are passed in x and y. The click count is passed in clicks. The triggersPopup flag indicates if this event causes a pop-up menu to appear on this pla_orm.

•  Two commonly used methods in this class are getX( ) and getY( ). These return the X and Y coordinates of the mouse within the component when the event occurred. Their forms are shown here:

int getX( ) int getY( )

•  AlternaJvely, you can use the getPoint( ) method to obtain the coordinates of the mouse. It is shown here:

Point getPoint( )

It returns a Point object that contains the X,Y coordinates in its integer members: x and y.

(7)

MouseEvent Class

•  The translatePoint( ) method changes the locaJon of the event. Its form is shown here:

void translatePoint(int x, int y)

•  Here, the arguments x and y are added to the coordinates of the event.

•  The getClickCount( ) method obtains the number of mouse clicks for this event.

•  Its signature is shown here:

int getClickCount( )

•  getBuPon( ) method returns a value that represents the buPon that caused the event:

int getBuPon( )

•  The return value will be one of these constants defined by MouseEvent:

NOBUTTON BUTTON1 BUTTON2 BUTTON3

•  The NOBUTTON value indicates that no buPon was pressed or released.

Java SE 6 added three methods to MouseEvent that obtain the coordinates of the mouse relaJve to the screen rather than the component. They are shown here:

Point getLocaJonOnScreen( ) int getXOnScreen( )

int getYOnScreen( )

Referensi

Dokumen terkait

The flatMap method is applied using a lambda expression that returns an Optional instance of itself.. While not the same object, they

The ToDate() function parses the supplied input value using the JavaScript match() method and returns a JavaScript Date object that represents that value.. The match()

return true; // success } // end delete // --- // returns node with next-highest value after delNode // goes to right child, then right child’s left descendents private Node

Foreground and Background Colors • To set the background color of an applet’s window, use setBackground • To set the foreground color the color in which text is shown, for example,

Inner Classes • An inner class is a class defined within another class • Inner classes can be used to simplify the code when using event adapter classes • The following applet

Adapter Classes • Adapter class can simplify the creaHon of event handlers in certain situaHons • An adapter class provides an empty implementaHon of all methods in an event

Top – Level Containers • The layered pane is an instance of JLayeredPane • The layered pane allows components to be given a depth value • The layered pane holds the content pane

David Matuszek-upenn polymorhism.ppt 7 how to override a method • create a method in a subclass having the same signature as a method in a superclass • that is, create a method in a