• Tidak ada hasil yang ditemukan

PDF Programming in Java

N/A
N/A
Protected

Academic year: 2024

Membagikan "PDF Programming in Java"

Copied!
9
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)

Handling Keyboard Events

•  If you want to handle the special keys, such as the arrow or funcHon keys, you need to

respond to them within the keyPressed( ) handler. They are not available through keyTyped( )

•  To idenHfy the keys, you use their virtual key codes

•  For example, the next applet outputs the

name of a few of the special keys

(4)

Handling Keyboard Events

// Demonstrate some virtual key codes.

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*

<applet code="KeyEvents" width=300 height=100>

</applet>

*/

public class KeyEvents extends Applet implements KeyListener {

String msg = "";

int X = 10, Y = 20; // output coordinates public void init() {

addKeyListener(this);

}

public void keyPressed(KeyEvent ke) { showStatus("Key Down");

int key = ke.getKeyCode();

switch(key) {

case KeyEvent.VK_F1:

msg += "<F1>";

break;

(5)

Handling Keyboard Events

case KeyEvent.VK_F2:

msg += "<F2>";

break;

case KeyEvent.VK_F3:

msg += "<F3>";

break;

case KeyEvent.VK_PAGE_DOWN:

msg += "<PgDn>";

break;

case KeyEvent.VK_PAGE_UP:

msg += "<PgUp>";

break;

case KeyEvent.VK_LEFT:

msg += "<Left Arrow>";

break;

case KeyEvent.VK_RIGHT:

msg += "<Right Arrow>";

break;

}

(6)

Handling Keyboard Events

repaint(); }

public void keyReleased(KeyEvent ke) { showStatus("Key Up");

}

public void keyTyped(KeyEvent ke) { msg += ke.getKeyChar();

repaint();

}

// Display keystrokes.

public void paint(Graphics g) { g.drawString(msg, X, Y);

} }

Output is as follows:

(7)

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 listener interface

•  Adapter classes are useful when you want to

receive and process only some of the events that are handled by a parHcular event listener

interface

•  You can define a new class to act as an event

listener by extending one of the adapter classes

and implemenHng only those events in which you

are interested.

(8)

Adapter Classes

•  For example

–  MouseMo5onAdapter class has two methods, mouseDragged( ) and mouseMoved( )

–  The above 2 methods are the methods defined by the MouseMo5onListener interface

–  If you were interested in only mouse drag events, then you could simply extend

MouseMo5onAdapter and override mouseDragged( )

–  The empty implementaHon of mouseMoved( )

would handle the mouse moHon events for you

(9)

Adapter Classes

•  Commonly used Adapter classes are:

Referensi

Dokumen terkait

Each object in the game is able to move, which means we can create a generic GameObject class with an abstract method called move (as per Code Listing 15)....

This is the idea that a class provides certain methods (the interface ) to the code that uses its objects, so the outside code does not directly access the data structures of

programmer- defined classes class Bicycle { // Data Member private String ownerName; //Constructor: Initialzes the data member public Bicycle { ownerName = "Unknown"; } //Returns

JLabel and ImageIcon • NoFce that icons are specified by objects of type Icon, which is an interface defined by Swing • The easiest way to obtain an icon is to use the ImageIcon class

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,

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

• “Object” means a particular item that belongs to a class – Also called an “instance” • Example String s1 = "Hello"; – Here, String is the class, and the variable s1 and the value