|
In the following stepse, you will add JDK 1.1 event handler methods to NervousText so that you can program NervousText beans to change their run time (as opposed to design time) behavior. While defining properties allows you to change the way a bean looks or behaves while building an application, event handlers allow you to define methods which can be fired in a running application. Start by adding adding an event receiver so that you can change the direction the text is written in NervousText from the OurButton object. OurButton is already available in the beanbox. Step 1. Add
First add the JDK 1.1 event handler import statement after
import java.awt.event.*;You should now have the following package and import statments at the top of NervousText02.java:
Step 2. Add a Variable to Control Direction of Text Add a boolean toggle variable called lefttoright in the NervousText class after the line:booleanthreadSuspended = false
boolean lefttoright=true;The data field declartions for NervousText02 should now look like this:
Step 3. Create ActionEvent Handler Method
Create an action event receiver target called
Step 4. Modify NervousText's
Finally change the paint method to draw the nervous text in a
different direction depending on the value of lefttoright. To do
this, change the call to g.drawChars(separated, i,1,x_coord,y_coord);to:
The paint method now looks like this
Step 5. Build the JAR and Install in the BeanBox Compile NervousText02.java: javac -d . NervousText02.java Create the mainfest:
The resulting temporary manifest file should look like this: Manifest-Version: 1.0 Name: sun/beanbox/beans/NervousText02.class Java-Bean: TrueCreate the JAR file:
jar cfm NervousText02.jar manifest.tmp
sun/beanbox/beans/NervousText02.class
Remove the temporary manifest and install the JAR in the BeanBox jars directory (substituting your BDK installation directory for BDK_HOME: rm manifest.tmp cp -p NervousText02.jar BDK_HOME/beans/jars Step 6. Test NervousText02 in BeanBox
You'll be adding two beans to the BeanBox for this test:
To run the BeanBox, switch to the BDK_HOME/beans/beanbox
directory. Invoke
Now you want a mouse press on the Now use the Edit menu to choose Edit->Events->action->actionPerformed. Join the event line to NervousText02 by clicking over the NervousText02 component. You should then get a popup named EventTargetDialog. Select the changeDirection method and click on the OK button. Pressing OurButton should now cause the text displayed by the NervousText02 button to be reversed from right to left. Program Source Code The makefile for this lesson automates source code compilation, JAR file construction, and copying of JAR files to the appropriate BeanBox directory. You'll have to edit several of the variables in the makefile to indicate the location of your JDK 1.1 and BDK installation directories. You may want to look at the final source file for NervousText Bean, Version 02 to verify the changes you have made to the original NervousText.java file. | |||||||||||
|
| ||||||||||||