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

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 displays the string

“Mouse Pressed” in the status bar of the

applet viewer or browser when the mouse is pressed, without using inner classes

(4)

Inner Classes

// This applet does NOT use an inner class.

import java.applet.*;

import java.awt.event.*;

/*

<applet code="MousePressedDemo" width=200 height=100>

</applet> */

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

addMouseListener(new MyMouseAdapter(this));

} }

class MyMouseAdapter extends MouseAdapter { MousePressedDemo mousePressedDemo;

public MyMouseAdapter(MousePressedDemo mousePressedDemo) { this.mousePressedDemo = mousePressedDemo;

}

public void mousePressed(MouseEvent me) {

mousePressedDemo.showStatus("Mouse Pressed.");

} }

(5)

Inner Classes

•  The init( ) method of MousePressedDemo

instanMates MyMouseAdapter and provides this object as an argument to the

addMouseListener( ) method.

•  A reference to the applet is supplied as an

argument to the MyMouseAdapter constructor

•  This reference is stored in an instance variable for later use by the mousePressed( ) method

•  When the mouse is pressed, it invokes the

showStatus( ) method of the applet through the stored applet reference

•  In other words, showStatus( ) is invoked relaMve to the applet reference stored by

MyMouseAdapter

(6)

Inner Classes

•  The program in the previous slide can be improved by using inner classes

•  InnerClassDemo is a top-level class that extends Applet

•  MyMouseAdapter is an inner class that

extends MouseAdapter and has access to all of the variables and methods within the scope of that class

•  Therefore, the mousePressed( ) method can call the showStatus( ) method directly

(7)

Inner Classes

// Inner class demo.

import java.applet.*;

import java.awt.event.*;

/*

<applet code="InnerClassDemo" width=200 height=100>

</applet> */

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

addMouseListener(new MyMouseAdapter());

}

//Inner Class

class MyMouseAdapter extends MouseAdapter { public void mousePressed(MouseEvent me) { showStatus("Mouse Pressed");

} } }

(8)

Anonymous Inner Classes

•  An anonymous inner class is one that is not assigned a name

•  An anonymous inner class can facilitate the wriMng of event handlers

•  Following applet uses anonymous inner class for mouse event handling

•  As before, its goal is to display the string

“Mouse Pressed” in the status bar of the

applet viewer or browser when the mouse is pressed

(9)

Anonymous Inner Classes

// Anonymous inner class demo.

import java.applet.*;

import java.awt.event.*;

/*

<applet code="AnonymousInnerClassDemo" width=200 height=100>

</applet> */

public class AnonymousInnerClassDemo extends Applet {

public void init() {

addMouseListener(new MouseAdapter() {

public void mousePressed(MouseEvent me) { showStatus("Mouse Pressed");

} });

} }

(10)

Anonymous Inner Classes

•  AnonymousInnerClassDemo is one top-level class in this program

•  The init( ) method calls the addMouseListener( ) method

•  Its argument is an expression that defines and instanMates an anonymous inner class

•  The syntax new MouseAdapter( ) { ... } indicates to the compiler that the code between the braces defines an anonymous inner class

•  Furthermore, that class extends MouseAdapter. This new class is not named, but it is automaMcally instanMated when this expression is executed

•  Because this anonymous inner class is defined within the scope of AnonymousInnerClassDemo, it has access to all of the variables and methods within the scope of that class

•  Therefore, it can call the showStatus( ) method directly

Referensi

Dokumen terkait

Chapter 6 shows how first-class functions can simplify some classic Object Oriented design patterns, while Chapter 7 dives into function decorators — a special kind of

In the very next chapter, we will make the sort program more complex, implementing more effective algorithms and also making our code flexible, giving us the opportunity to learn

In typical imperative Python programs—including those that make use of classes and methods to hold their imperative code—a block of code generally consists of some outside loops (for

A class, in the context of Java, are templates that are used to create objects, and to define object data types and methods.. For

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

The type of the event is specified by type, and the component that has been added to or removed from the container is comp • You can obtain a reference to the container that generated

Its general form is shown here: Window getWindow • WindowEvent also defines methods that return the opposite window when a focus or acWvaWon event has occurred, the previous window

Keyword What It Does abstract Indicates that the details of a class, a method, or an interface are given elsewhere in the code.. assert Tests the truth of a condition that the