
• Creating an Interface
Follow the directions
Create a project InterfaceTest
Create an interface InterFaceTest
Use the following code to create an interface
public class myFirstInterFace(){
pubic void aboutMe();
}
Save it and compile it
• Implementing the interface
Following the directions
Create a class Test
add following to your class declaration
implements myFirstInterFace
add following code
public void aboutMe(){
System.out.println(“My first interface implemented by Test”);
}
Create main method
Create an object of Test
Call aboutMe method of class Test.
If you see “My first interface implemented by Test “ in output window you have successfully
implemented your first interface
• Learning from errors
Comment the method aboutMe() method in your last program and look at the error in class view
pane
• Event Handling
Knowing the events related to a component
Follow the directions
Create an applet or a frame using JBuilder
Open design tool
Add a command button and rename it to Click It
Click events tab (located at bottom right of content pane)
You can see the list of events related to this component
Return to the source code and add the following
[To the class definition]
implements ActionListener [to the class definition]
[at the end of your jbinit() method]
button1.addActionListener(this)
[Any where in the class but out of any method or constructor]
public void actionPerformed(ActionEvent event){
System.out.println("The Button is pressed");
}
Create an object of your frame in main method set its size and call its show method
Compile your program and click the button if you see “The Button is Pressed” you have
successfully implemented your first event handler
Re-Implement the code using an anonymous class
• Create a frame and
Add three text fields with label Num1, Num2, Sum
Add a two buttons with labels Reset, Calculate
Write code to perform the following when Reset button is pressed
Clear all the text fields
Write code to perform the following when Calculate button is pressed
Add values in Num1 and Num2 and display result in Sum
• Learning from JBuilder
Open source code of previous program
Some where in your code write name of a button you have created and press (. Dot )
Find method addActionListener ….see other methods for adding other listener , did you find any
method for adding some other listener
• KeyListener interface has following methods
public void keyPressed(KeyEvent e);
public void keyReleased(KeyEvent e);
public void keyTyped(keyEvent e);
there are lots of constant which you can see by typing KeyListener (. Dot ) in JBuilder
• Re-implement your calculation program with following modification
Remove the buttons
Implement the key listener such that
When escape key is pressed the field should be cleared
When enter key is pressed in Num1 the focus should be transferred to next field
When enter key is pressed in Num2 the result should be displayed in Sum
J
Ja
av
va
a-
-P
Pr
ro
og
gr
ra
am
mm
mi
in
ng
g
(
(D
Da
ay
y-
-0
06
6
E
Ex
xe
er
rc
ci
is
se
e-
-0
01
1)
)