You are here: Recent News » Projects » AsWing » AsWing tutorials » JFrame JLabel FlowLayout and MCIcon

 

JFrame JLabel FlowLayout and MCIcon

Tags : ,

This tutorial will show you how to use the JFrame , JLabel , the FlowLayout and the MCIcon to create a MessageBox. Lets create our main class MessageBox.as which extends JFrame. We will need a JLabel instance to display the message and two JButton instances for the ok and cancel buttons .We will need to create two JPanel instances one for the JLabel which will display our message and icon and one for the ok and cancel buttons.

 
import org.aswing. *;
import org.aswing.utils.Delegate;
import org.aswing.geom.Dimension;
class MessageBox extends JFrame
{
	private var buttonsPanel : JPanel;
	private var labelPanel : JPanel;
	private var okBtn : JButton;
	private var cancelBtn : JButton;
	private var messageDisplay : JLabel;
	private var title : String;
	private var message : String;
	public functiopublic function MessageBox (title : String , message : String,)
	{
		super (_root, true);
		this.title = title;
		this.message = message;
	}
	public static function main (Void) : Void
	{
		var myWindow : MessageBox = new MessageBox ( "Information",
		"ASWing ROCKS!!.\nLike you did'nt know that already.");
		myWindow.setLocation (50, 50);
		myWindow.setSize (300, 150);
		myWindow.setResizable (false);
		myWindow.show ();
	}
}

The title and the message string variables store the value for the title bar for our frame and the message to be displayed in the JLabel component respectively. The constructor takes the title and message as parameters. The main method creates an instance of the MessageBox and passes in the two string parameters. We then set the location and size for our frame and make sure its not resizable.

Next we instantiate our button instances and our buttonPanel instance

buttonsPanel =new JPanel();
okBtn = new JButton ("Ok");
cancelBtn = new JButton ("Cancel");

Next we set the preferred size for our buttons.

okBtn.setPreferredSize(75,22);
cancelBtn.setPreferredSize(75,22);

Here we use the setPreferredSize method and not setSize because the if a container has a layout then it will use the components getPreferredSize() but if a container has an EmptyLayout then we can use setSize method to set the components dimensions. We will use the FlowLayout to arrange the buttons and the label in their respective panels. A FlowLayout arranges components in a left-to-right flow, much like lines of text in a paragraph. Flow layouts are typically used to arrange buttons within a panel. It will arrange buttons left to right until no more buttons are able to fit on the same line and each line is centered.

buttonsPanel.setLayout(new FlowLayout(FlowLayout.RIGHT,20,8));
buttonsPanel.append(okBtn);
buttonsPanel.append(cancelBtn);

We set our FlowLayout such that the buttons are right aligned and the horizontal gap between components is 20 and the vertical gap between the components is 8. We then add it to the buttons panel. We create the same layout for the labelPanel and the JLabel instance to the pane

labelPanel = new JPanel();
messageDisplay=new JLabel(message);
labelPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
labelPanel.append(messageDisplay);

We now add the two JPanel instances to our frame and set the title.

getContentPane ().append (labelPanel, BorderLayout.CENTER);
getContentPane ().append (buttonsPanel, BorderLayout.SOUTH);
setTitle(title);

Next we create a method to set our icon for the label instance.

public function setDisplayIcon(icon:Icon):Void{
   messageDisplay.setIcon(icon);
}

Now in our main method we create an MCIcon instance and pass that icon instance to the method created above. The MCIcon lets us create an icon by passing in a linakge id of a movieclip. In this way we can create our icon in gimp / photoshop , use swfmill to create the swf which contains the image as a movieclip with a linkage id , and use that movieclip as an icon in ASWing. We can use the MCPanel for similar usage but we will talk more about MCPanel in detail in another tutorial.

var info:MCIcon =new MCIcon("INFO",51,43);
myWindow.setDisplayIcon(info);

The other two parameters passed to the MCIcon constructor are the width and the height of the movieclip. Below is a screenshot of a slightly modified version of this example. You can of course extend this example to create event listenters for the buttons to call other methods that you may need to call. Hope you have a better understanding of how to create more complex GUI`s using panels and layout managers

MessageBox .as


 
 /**
* @author firdosh
*/
import org.aswing. *;
class MessageBox extends JFrame
{
	private var buttonsPanel : JPanel;
	private var labelPanel : JPanel;
	private var okBtn : JButton;
	private var cancelBtn : JButton;
	private var title : String;
	private var message : String;
	private var messageDisplay : JLabel;
	private var icon : MCIcon;
	public function MessageBox (title : String , message : String)
	{
		super (_root, true);
		this.title = title;
		this.message = message;
		buttonsPanel = new JPanel ();
		okBtn = new JButton ("Yes");
		cancelBtn = new JButton ("No");
		okBtn.setPreferredSize (75, 22);
		cancelBtn.setPreferredSize (75, 22);
		buttonsPanel.setLayout (new FlowLayout (FlowLayout.RIGHT, 20, 8));
		buttonsPanel.append (okBtn);
		buttonsPanel.append (cancelBtn);
		labelPanel = new JPanel ();
		messageDisplay = new JLabel (message);
		labelPanel.setLayout (new FlowLayout (FlowLayout.CENTER));
		labelPanel.append (messageDisplay);
		getContentPane ().append (labelPanel, BorderLayout.CENTER);
		getContentPane ().append (buttonsPanel, BorderLayout.SOUTH);
		setTitle (title);
	}
	public function setDisplayIcon (icon : Icon) : Void
	{
		messageDisplay.setIcon (icon);
	}
	public static function main (Void) : Void
	{
		var myWindow : MessageBox = new MessageBox ("Information", "ASWing ROCKS!!.\nDon`t you agree ;).");
		var info : MCIcon = new MCIcon ("INFO", 51, 43);
		myWindow.setDisplayIcon (info);
		myWindow.setLocation (50, 50);
		myWindow.setSize (300, 150);
		myWindow.setResizable (false);
		myWindow.show ();
	}
}

projects/aswing/tutorials/messagebox_jframe_flowlayout_jlabel_mcicon.txt · Last modified: 2007/02/20 08:18 by niko