JLabel s is a built-in Java Swing class that lets you display information on a JFrame.
javax.swing.JLabel
Constructors Description JLabel ()Creates a JLabel instance with no image and with an empty string for the title. JLabel (Icon image)Creates a JLabel instance with the specified image. JLabel (Icon image, int horizontalAlignment)Creates a JLabel instance with the specified image and horizontal alignment. JLabel (String text) Creates a JLabel instance with the specified text. JLabel (String text, Icon icon, int horizontalAlignment)Creates a JLabel instance with the specified text, image, and horizontal alignment. JLabel (String text, int horizontalAlignment)Creates a JLabel instance with the specified text and horizontal alignment.
Methods Description String getText ()
Returns the text string that the label displays. void setText (String text)
Defines the single line of text this component will display. Icon getIcon ()
Returns the graphic image (glyph, icon) that the label displays. void setIcon (Icon icon)
Defines the icon this component will display. int getVerticalTextPosition ()
Returns the vertical position of the label’s text, relative to its image. void setVerticalTextPosition
(int textPosition)
Sets the vertical position of the label’s text, relative to its image.
ImageIcon
javax.swing.ImageIcon
The ImageIcon class defines objects representing small fixed-size pictures that are used typically to decorate components. This class implements the Icon interface.
Constructors Description ImageIcon ()
Creates an uninitialized image icon. ImageIcon (byte[] imageData)
Creates an ImageIcon from an array of bytes which were read from an image file containing a supported image format, such as GIF or JPEG. ImageIcon (byte[] imageData, String description)
Creates an ImageIcon from an array of bytes which were read from an image file containing a supported image format, such as GIF or JPEG. ImageIcon (Image image)
Creates an ImageIcon from an image object. ImageIcon (Image image, String description)
Creates an ImageIcon from the image. ImageIcon (String filename)
Creates an ImageIcon from the specified file. ImageIcon (String filename, String description)
Creates an ImageIcon from the specified file. ImageIcon (URL location)
Creates an ImageIcon from the specified URL. ImageIcon (URL location, String description)
Creates an ImageIcon from the specified URL.
Example
import javax.swing.*;
import java.awt.*;
public class JFrameDemo extends JFrame
{
public JFrameDemo()
{
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
ImageIcon ii = new ImageIcon("bulg.jpg");
JLabel jl1 = new JLabel("Image", ii, JLabel.CENTER);
jl1.setVerticalTextPosition(JLabel.BOTTOM);
jl1.setHorizontalTextPosition(JLabel.CENTER);
cp.add(jl1);
setTitle("Demo");
//setSize(300,500);
//setLocation(150,250);
setBounds(100,200,400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args)
{
new JFrameDemo();
}
}