This tutorial helps you understand the BorderLayout class and the JButton component. We start of by creating an empty class. In my case I am going to name my class as JButtonExample.as . In order for us to create or display a button we need to add it to a container. The JWindow class extends the Container class so we can add a component to the JWindow. In our example we will simply extend our class so that it is a subclass of JWindow. (Note : We will be talking more in depth about JWindow in other tutorials ).
class JButtonExample extends JWindow{ }
In order for us to use the JWindow class we must import it. We also need to import the JButton , BorderLayout classes and import the org.aswing.utils package which contains several usefull classes , from which we will be using the Delegate class
import org.aswing.BorderLayout; import org.aswing.JButton; import org.aswing.JWindow; import org.aswing.utils.*;
We can now create our main method and constructor for the class. In the main method we create an instance our class.
public function JButtonExample(Void){ } public static function main(Void):Void{ var myWindow : JButtonExample = new JButtonExample (); }
We now make a call to our super coonstructor in the JWindow class.
public function JButtonExample(Void){ super(_root, true); }
The JWindow constructor can accept either a MovieClip or another JWindow object as the parent or owner. If a null object is passed then it takes _root to be its owner. The second parameter is a Boolean value which determines wether the JWindow instance is modal or not. What this means that if a JWindow instance is modal then other windows cannot be active at the same moment. We will talk in more detail about this property in other tutorials. Next we create five instances of JButton and instantiate them in the constructor of our class.
private var jbutton1:JButton; private var jbutton2:JButton; private var jbutton3:JButton; private var jbutton4:JButton; private var jbutton5:JButton; public function JButtonExample(Void){ super(_root, true); jbutton1 = new JButton("JButton1"); jbutton2 = new JButton("JButton2"); jbutton3 = new JButton("JButton3"); jbutton4 = new JButton("JButton4"); jbutton5 = new JButton("JButton5"); ...........................
When we pass a String to a JButton constructor that String becomes the text / label for the JButton instance. In order for us to add components to the JWindow instance we need to use the getContentPane() method which returns a Container which holds all the children for that JWindow instance.
getContentPane().append(jbutton1, BorderLayout.NORTH);
The above line of code adds the jbutton1 instance to our class and the second parameter tells the JWindow where to place the jbutton1 component. Lets talk a little bit more about BorderLayout class.
The BorderLayout class helps us to layout components within the JWindow. Below is a diagram showing how the class places components within the JWindow component.
Below are the class properties which help us determine where to place a component
BorderLayout.NORTH BorderLayout.SOUTH BorderLayout.CENTER BorderLayout.EAST BorderLayout.WEST
Therefore in our example our jbutton1 component will be places in the NORTH location. Next we make set our size and location for our window and make it visible. We set these properties in our main function after we have instantiated our JButtonExample class.
myWindow.setLocation (50, 50); myWindow.setSize (300, 300); myWindow.show ();
Now we can run our example and we should be able to see a panel with the button on top.
Lets talk a little about the event dispatching design in ASWing.The event dispatching is similar to the MM V2 component architecture where the EventDispatcher class takes care of all of the work. Although in ASWing the main Component class which all components are subclasses of extends EventDispatcher so there is no need to do
EventDispatcher.initialize(this);
So lets setup our function which will get called when jbutton1 is pressed.
private function createEast(eventObj:Event):Void{ getContentPane().append(jbutton2, BorderLayout.EAST); }
The Event Object passed to the function can be used to get the component which called this function and what type of event was fired
eventObj.getSource(); //Returns a Component Object eventObj.getType(); //Returns a String which is the type of event fired.
Now we create an eventlistener for our jbutton1 instance in our constructor method right after instantiating our instance.
jbutton1 = new JButton("JButton1"); getContentPane().append(jbutton1, BorderLayout.NORTH); jbutton1.addEventListener(JButton.ON_PRESS,Delegate.create(this,createEast));
The JButton.ON_PRESS is the type of event and createEast is the name of the method to be called. Now when we run our example we will see that when we click on jbutton1 jbutton2 is added to the east location of the layout.
Similarly we add the other button instances to other locations of the JWindow instance using the different properties of the BorderLayout. Below is the full source code to the provided example. Hope this tutorial has helped you get a better understanding of ASWing.
/** * @author firdosh */ import org.aswing.BorderLayout; import org.aswing.Event; import org.aswing.JButton; imporimport org.aswing.JWindow; import org.aswing.utils. *; class JButtonExample extends JWindow { private var jbutton1 : JButton; private var jbutton2 : JButton; private var jbutton3 : JButton; private var jbutton4 : JButton; private var jbutton5 : JButton; public function JButtonExample () { super (_root, true); jbutton1 = new JButton ("JButton1"); jbutton2 = new JButton ("JButton2"); jbutton3 = new JButton ("JButton3"); jbutton4 = new JButton ("JButton4"); jbutton5 = new JButton ("JButton5"); jbutton1 = new JButton ("JButton1"); getContentPane ().append (jbutton1, BorderLayout.NORTH); jbutton1.addEventListener (JButton.ON_PRESS, Delegate.create (this, createEast)); jbutton2.addEventListener (JButton.ON_PRESS, Delegate.create (this, createSouth)); jbutton3.addEventListener (JButton.ON_PRESS, Delegate.create (this, createWest)); jbutton4.addEventListener (JButton.ON_PRESS, Delegate.create (this, createCener)); } private function createEast (eventObj : Event) : Void { getContentPane ().append (jbutton2, BorderLayout.EAST); } private function createSouth (eventObj : Event) : Void { getContentPane ().append (jbutton3, BorderLayout.SOUTH); } private function createWest (eventObj : Event) : Void { getContentPane ().append (jbutton4, BorderLayout.WEST); } private function createCener (eventObj : Event) : Void { getContentPane ().append (jbutton5, BorderLayout.CENTER); } public static function main (Void) : Void { var myWindow : JButtonExample = new JButtonExample (); myWindow.setLocation (50, 50); myWindow.setSize (300, 300); myWindow.show (); } }
Discussion