Swing
JLabel
This
tutorial describes the Swing JLabel class.
The
JLabel Class
Plain
JLabel
JLabel
with Image
JLabel
with Image and Text
Other
JLabel Methods
The
JLabel Class
You can
use the JLabel
class to display text and images. A label exist to display information
to the user and so a JLabel
is uneditable. There are five constructors to choose from when creating a
label.
|
creates
a label with text displayed
|
|
creates
a label and justifies the text according to position .
|
|
an
image is shown on the button
|
|
creates
an label showing an image and justifies the image according to position .
|
|
creates
a label showing text and an image, justified according to position .
|
Values you can
use when a position argument is expected are:-
JLabel.LEFT
JLabel.RIGHT
JLabel. Center
The value of position
you choose affects the relative alignment of the text and image within the
painting area of the label.
Plain
JLabel
To create a
plain label, use a line of code such as:-
JLabel
aLabel = new JLabel(" I am a plain label");
To create a
plain label, with a specific text alignment use a line of code such as:-
JLabel
aLabel = new JLabel(" I am a plain label", JLabel.LEFT);
or
JLabel
aLabel = new JLabel(" I am a plain label", JLabel.RIGHT);
or
JLabel
aLabel = new JLabel(" I am a plain label", JLabel.CENTER);
Note:
-- You will only see a change in the alignment of text
within the label if the label's painting area is larger than the text
within the label. The painting area may be the exact dimensions of the
text, in which case, the alignment value will appear to do nothing.
For example, if a
label is added to the content pane of an applet
window, the painting area of the label takes up the whole area of the applet's
content pane . The label shown below is drawn
the exact width of the applet window's content pane .
The label was drawn with a border around it to show you the painting area.
If a label is added
to a panel, the label's painting area is the exact size it needs to be to
display it's contents.
|
JLabel
with Image
To create a
label with an image we need to create an Icon . We can use
the ImageIcon class for this:-
ImageIcon
anIcon = new ImageIcon ("spiral.gif");
then we can
use that variable that references the ImageIcon object in the
argument for the label's constructor:-
JLabel aLabel = new
JLabel(anIcon);
Alternatively,
if we don't need a variable that references the ImageIcon
object we could use a single line of code as follows:-
JLabel aLabel
= new JLabel(new ImageIcon ("spiral.gif"));
Don't
forget to place your image file in the same folder as you class file.
Important:
-- Using images in applets? Then read this
and
instead of creating a new ImageIcon
instance with the line:-
ImageIcon
anIcon = new ImageIcon ("spiral.gif");
We
would use:-
ImageIcon
anIcon = new ImageIcon (getImage(getCodeBase(),"spiral.gif"));
You
must also import an extra package:-
import
java.net.*
|
To create a label,
with a specific image alignment use a line of code such as:-
JLabel
aLabel = new JLabel(new ImageIcon ("spiral.gif"), JLabel.RIGHT);
This will place
the image on the right side of the label's painting area. You can
also use JLabel.LEFT and JLabel. CENTER.
Note:
-- You will only see a change in the alignment if the
label's painting area is larger than the text within the label. The
painting area may be the exact dimensions of the text, in which case, the
alignment value will appear to do nothing. See the note above for more
details.
|
JLabel with
Image
& Text
To create a label
that displays both text and an image, use a line of code such as:-
JLabel
aLabel = new JLabel("I am a label with an image and a caption"
new ImageIcon ("spiral.gif"), JLabel.LEFT);
If
you are writing an applet, you should use these lines instead:-
ImageIcon
anIcon = new ImageIcon ( getImage(getCodeBase()," "spiral.gif" ") );
JLabel
aLabel = new JLabel
("I am a label with an image and a caption",
anIcon , JLabel.LEFT);
and
include the following package:-
import
java.net.*
Don't
forget to place your image file in the same folder as you class file.
Note:
-- The alignment values JLabel.LEFT, JLabel.RIGHT
and JLabel.CENTER position the image and text relative to
the painting area of the label. You will only see a change in the alignment if the
label's painting area is larger than the contents of the label. The
painting area may be the exact dimensions of the label's contents, in which case, the
alignment value will appear to do nothing.
|
~Now
try the activity~
Activity A |
Create an applet with one label added to the applet's content
pane. The label should display both a text and an image, (right-aligned.)
- Now change and recompile your code so that the image and text is
center-aligned.
- Now change and recompile your code so that the label is added to a
panel which is added to the content pane of the applet. Is
there any observable change in the way the label is painted?
Note : You can use my
image
file if you wish or find a gif of your own.
|
Other
JLabel Methods
Here are some
methods we can use with the JLabel class. You should note
that this is not a full list, there are lots of other methods. You
should also check out the inherited JComponent
class methods.
-
setText(String
s)
-
getText()
|
Set
or get the text displayed by the label.
|
|
Set
the horizontal position of the label's text relative to the label's image
using as the argument: JLabel.LEFT ,
JLabel.CENTER
or JLabel.RIGHT .
|
|
Set
the vertical position of the label's text relative to the label's image using
as the argument: JLabel.TOP ,
JLabel.CENTER (the default), and
JLabel.BOTTOM .
|
~Now
try the activity~
Activity B |
Using the applet you created for activity A and using appropriate
methods from the JLabel and JComponent class
...
- Set the image position to
the right of the text in the label.
- Set the font color of the
label to any color of your choice, (apart from black).
- Set the font to Arial,
bold, italic and 14 points in size. The Font class is explained
in the Graphics tutorial.
|
|