Thursday, July 8, 2010

JLabel  

0 comments

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.

What next?

You can also bookmark this post using your favorite bookmarking service:

Related Posts by Categories



0 comments: to “ JLabel

Extension Factory Builder