

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to java interfaces and event handling. It covers creating interfaces, implementing them, and using them as abstract data types. It also discusses event handling techniques, using interfaces directly or through adapter classes, and handling events using anonymous classes. Examples of creating a first event handler and handling events for multiple buttons.
Typology: Slides
1 / 25
This page cannot be seen from the preview
Don't miss anything!


















An Introduction
Outline
Interfaces..cont..
Cut Copy Paste
Clipboard capacity
Creating Interfaces
public private protected Extends interface EditAble { - - - - -body of interface - - - - - } public interface EditAble{ - - - - body of interface - - - } public interface EditAbleGraphics extends EditAble{- - - -body- - }
Methods are public by default public void cut(); public void paste(); public void copy(); Version- public void cut(int start,int end);
Constants are static and final by default Must be initialized when declared int LIMIT=2;
Interface-Example
Using an interface
public class TrafficSignal implements BasicColors{ -----body------- }
package InterfaceTest;
public class TrfficSignal implements BasicColors{ public void changeToRed(){ System.out.println(“The color of the signal is RED”); } public void changeToGreen(){ System.out.println(“The color of the signal is GREEN”); } public void changeToBlue(){ System.out.println(“The color of the signal is BLUE”); } }
Using interfaces as abstract type
Some interfaces provided by Java
Play() Stop() Loop()
Implemented by classes for providing playback functionality
Implemented by the classes that want to become a layout manager
run()
Implemented by any class whose instances are intended to be executed by a thread
Event Handling
When a button of an applet is pressed A button press event object is generated Applet translates the event and try to find some handler in the applet If a handler in the program is found the event is handed over to it If no handler is found the default handler is used
Execution of the code
complete program
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class myApplet extends Applet implements ActionListener{ //class declaration
private Button button1 = new Button();
public void init () {
button1.setText(“Click It");
button1.addActionListener(this); //specifying the action listener
add(button1);
}
//implementing method of ActionListener
public void actionPerformed(ActionEvent event){
System.out.println("The Button is pressed");
} }
Handling events for more than one button
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class myApplet extends Applet implements ActionListener{
private Button button1 = new Button();
private Button button2 = new Button();
public void init () {
button1.setText(“Click It");
button1.addActionListener(this);
button2.addActionListener(this);
add(button2);
add(button1); }
//implementing method of ActionListener
public void actionPerformed(ActionEvent event){
System.out.println("The Button is pressed");
} }
Solution-
class button1_listener implements ActionListener{
public void actionPerformed(ActionEvent e){ System.out.println(“Button 1 is pressed “); } }
class button2_listener implements ActionListener{
public void actionPerformed(ActionEvent e){ System.out.println(“Button 2 is pressed “); } }