Wednesday, February 25, 2009

JLabel



JLabels in Swing can contain text, images or both.
JLabel do not generate an action event though
it works on Mouse Events.Like MouseMotion,MouseListener etc.
We can also add non-functional labels in our interface(GUI).
You can also specify the position of the text relative to the image.
Also we can align,specify border,size and many more properties.
Constructors:-JLabel class has following Constructors

JLabel() // Creates a JLabel without an image and with empty string title
JLabel(Icon icon) //Creates a JLabel instance with specified image as title
JLabel(Icon image,int horizontalAlignment)// Creates a JLabel object with image and alignment
JLabel(String text)//JLabel with some text as title
JLabel(String text,Icon image,int horizontalAlignment)
JLabel(String text ,int horizontalalignment)
Example:-
The following exmaple is for simple label showing only text:-

import javax.swing.*;

public class SimpleJLabel extends JFrame{

public static void main(String[] args) {

JLabel label = new JLabel("A Simple Text Label");

JFrame frame = new JFrame();

frame.getContentPane().add(label);

frame.pack();

frame.setVisible(true);
}
}
Example:-
The Following Example shows setting up an Icon on Label

import javax.swing.*;

public class SimpleJLabel extends JFrame{

public static void main(String[] args) {

JLabel label = new JLabel();

label.setIcon(new ImageIcon("e1.png"));

JFrame frame = new JFrame();

frame.getContentPane().add(label);

frame.pack();

frame.setVisible(true);

}

}


You can also set many more properties like mnemonics,tooltiptext alignment etc.

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP