JLabel
Labels: jlable, swings 0 commentsJLabels 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: to “ JLabel ”
Post a Comment