Page 3
We don't need anymore to create controls which are internally accessible. We will just create a Button control to convert the temperature, and the label which indicates that the temperature unit of the input field is celsius.
var _btn = new Button("Convert", _input.width);
var celsiusLabel:Label = new Label("celsius");
All controls are built now. To add them at one time into the Popup container we use the addGraphicElements method. The following line of code illustrates this process:
addGraphicElements(_input, celsiusLabel, _btn, _result);
Well, the widget is not so bad, but it is not interactive yet. We must implement a function to convert the temperature. Let's call it convertTemp. The algorithm of the convertTemp function is quite simple. When the user clicks the "convert" button, the program reads the input field value as a Number, and writes the converted value into the result label field.
To add mouse actions, we use the UIMouseEvent class: import org.flashapi.swing.event.*;
The event model of SPAS 3.0 is exactly the same as the AS3 event model. But it is better to use the SPAS 3.0 integrated tools to manipulate events, than to use an other system. SPAS 3.0 has its own event sub-system to prevent leak of memory due to event management. That's why we use the eventCollector property of the Popup class to register the button event:
eventCollector.addEvent(_btn, UIMouseEvent.CLICK, convertTemp);
When the user clicks the button, it will call the convertTemp function which is based on the algorithm described above:
private function convertTemp(event:UIMouseEvent):void {
var temp:Number = Number(_input.label);
_result.label = String(temp * 1.8 + 32) + " fahrenheit";
}
Well, we should have a 28 lines of code class. If not, here is the complete CelsiusConverter class:
package {
import org.flashapi.swing.*;
import org.flashapi.swing.event.*;
public class CelsiusConverter extends Popup {
public function CelsiusConverter() {
super("Convert Celsius to Fahrenheit", 250);
initialize();
}
private var _result:Label;
private var _input:TextInput;
private function initialize():void {
autoHeight = true;
padding = horizontalGap = verticalGap = 10;
_input = new TextInput("0");
_input.maxChars = 5;
_input.restrict = "0-9";
_result = new Label("fahrenheit");
var _btn = new Button("Convert", _input.width);
var celsiusLabel:Label = new Label("celsius");
addGraphicElements(_input, celsiusLabel, _btn, _result);
eventCollector.addEvent(_btn, UIMouseEvent.CLICK, convertTemp);
}
private function convertTemp(event:UIMouseEvent):void {
var temp:Number = Number(_input.label);
_result.label = String(temp * 1.8 + 32) + " fahrenheit";
}
}
}
| » Level Intermediate |
|
Added: 2008-12-11 Rating: 1 Votes: 1 |
| » Author |
| Pascal Echemann is a Web Developer and Project Manager for "Bananatree Design" on the French Riviera. He also is the creator of the "Swing Package for ActionScript 3.0" (SPAS 3.0). |
| » Download |
| Download the files used in this tutorial. |
| Download (118 kb) |
| » Forums |
| More help? Search our boards for quick answers! |
-
You must have javascript enabled in order to post comments.


Comments
There are no comments yet. Be the first to comment!