• Tidak ada hasil yang ditemukan

PDF Programming in Java

N/A
N/A
Protected

Academic year: 2024

Membagikan "PDF Programming in Java"

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

Adapter Classes

•  Commonly used Adapter classes are:

(4)

Adapter Classes

•  The following program demonstrates an adapter

•  It displays a message in the status bar of an applet viewer or browser when the mouse is clicked or dragged

•  However, all other mouse events are silently

ignored

(5)

Adapter Classes

•  The following program demonstrates an adapter

•  The program has three classes

•  AdapterDemo extends Applet

•  Its init( ) method creates an instance of

MyMouseAdapter and registers that object to receive noKficaKons of mouse events

•  It also creates an instance of

MyMouseMo5onAdapter and registers that object to receive noKficaKons of mouse

moKon events

(6)

Adapter Classes

•  MyMouseAdapter extends MouseAdapter and overrides the mouseClicked( ) method

•  The other mouse events are silently ignored by code inherited from the MouseAdapter class

•  MyMouseMo5onAdapter extends

MouseMo5onAdapter and overrides the mouseDragged( ) method

•  The other mouse moKon event is silently ignored by code inherited from the

MouseMo5onAdapter class

•  Note that both of the event listener classes save a reference to the applet

•  This informaKon is provided as an argument to

their constructors and is used later to invoke the

showStatus( ) method

(7)

Adapter Classes

// Demonstrate an adapter.

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*

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

</applet>

*/

public class AdapterDemo extends Applet { public void init() {

Init() is used to iniKalise the applet. It’s a predefined funcKon of Applet class

addMouseListener(new MyMouseAdapter(this));

addMouseMotionListener(new MyMouseMotionAdapter(this));

}

}

(8)

Adapter Classes

// Demonstrate an adapter.

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*

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

</applet>

*/

public class AdapterDemo extends Applet { public void init() {

addMouseListener(new MyMouseAdapter(this));

The above statement is Registering for the listener for the Mouse event.

this is a reference to the applet itself.

addMouseMotionListener(new MyMouseMotionAdapter(this));

The above statement is Registering for the listener for the MouseMoKon event. this is a reference to the applet itself.

} }

(9)

Adapter Classes

class MyMouseAdapter extends MouseAdapter { AdapterDemo adapterDemo;

Object of the applet which has registered for the mouse and mousemoKon listeners.

This is the source of the event

public MyMouseAdapter(AdapterDemo adapterDemo) { this.adapterDemo = adapterDemo;

}

addMouseListener() calls the constructor of MyMouseAdapter class enabling an associaKon between the acKve applet and the listener interface via an object of this class

public void mouseClicked(MouseEvent me) {

adapterDemo.showStatus("Mouse clicked");

}

•  The adapter class enables to override only some of the funcKons defined by the mouse listener interface. Hence, the adapter class only overrides mouseClicked().

•  AdapterDemo points to the acKve applet and the showStatus() is used to print

“MOUSE CLICKED” on the status bar of the applet

}

(10)

Adapter Classes

class MyMouseMotionAdapter extends MouseMotionAdapter { AdapterDemo adapterDemo;

Object of the applet which has registered for the mousemoKon listeners. This is the source of the event

public MyMouseMotionAdapter(AdapterDemo adapterDemo) { this.adapterDemo = adapterDemo;

}

addMouseMoKonListener() calls the constructor of MyMouseMoKonAdapter class enabling an associaKon between the acKve applet and the listener interface via an object of this class

// Handle mouse dragged.

public void mouseDragged(MouseEvent me) { adapterDemo.showStatus("Mouse dragged");

}

•  The adapter class enables to override only some of the funcKons defined by the

mousemoKon listener interface. Hence, the adapter class only overrides mouseDragged().

•  AdapterDemo points to the acKve applet and the showStatus() is used to print “MOUSE DRAGGED” on the status bar of the applet

}

}

Referensi

Dokumen terkait

HelloWorld class and the main method as public , which is the most common arrangement for classes and methods.. • A package is a set of

(Hey, how about that? The word javac stands for “Java compiler!”) As a Java programmer, you often tell your computer to build some new object code.. Your computer fulfills this

If, at some point in the code block of a loop, you decide you need to immediately leave the loop, this can be done using a Java break statement. When a break statement

Note that in order for the code to compile, the FictionalCharacter class or one of its super- classes (or, in general, ancestors in the inheritance heterarchy) must contain the

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)....

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,

• 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

A Better Way As observant Java programmers, the minute we set our eyes on this code we’d quickly turn it into something more concise and easier to read, like this: