In Java, JLabel
is a class that represents a component that displays a single line of read-only text or an image. It can be used to display static text, images, or a combination of both.
Here’s a basic example of creating a JLabel
:
import javax.swing.*; public class MyLabel extends JFrame { public static void main(String[] args) { JFrame frame = new JFrame("My Frame"); JLabel label = new JLabel("Hello, World!"); // Creates a JLabel with the text "Hello, World!" frame.add(label); // Adds the label to the frame frame.pack(); frame.setVisible(true); } }
In this example, we first import the javax.swing
package which contains the JLabel
class. Then we create a JFrame
object called frame
, and a JLabel
object called label
with the text “Hello, World!”.
Finally, we add the label to the frame using the add()
method, and set the frame to be visible using the setVisible()
method. When we run the program, a window with the text “Hello, World!” will be displayed.
JLabel
can also display images. Here’s an example of how to create a JLabel
that displays an image:
import javax.swing.*; public class MyLabel extends JFrame { public static void main(String[] args) { JFrame frame = new JFrame("My Frame"); ImageIcon icon = new ImageIcon("image.jpg"); // Creates an ImageIcon object with the specified image JLabel label = new JLabel(icon); // Creates a JLabel with the ImageIcon frame.add(label); // Adds the label to the frame frame.pack(); frame.setVisible(true); } }
n this example, we create an ImageIcon
object using an image file called “image.jpg”, and then create a JLabel
object with the ImageIcon
. Finally, we add the label to the frame and set the frame to be visible. When we run the program, the specified image will be displayed in a window.
JLabel class declaration:
The JLabel
class is part of the Java Swing framework and is used to create a component that displays a single line of read-only text or an image. The class declaration for JLabel
is as follows:
public class JLabel extends JComponent implements SwingConstants, Accessible
The JLabel
class extends the JComponent
class, which is the base class for all Swing components, and implements the SwingConstants
and Accessible
interfaces.
The SwingConstants
interface defines constants that are used to specify the alignment of the label’s contents. For example, SwingConstants.CENTER
can be used to center the label’s contents horizontally.
The Accessible
interface is used to provide accessibility information for the label.
The JLabel
class provides several constructors, which allow you to create a label with text, an icon, or both. It also provides methods for setting and getting the text and icon displayed by the label, as well as methods for setting the alignment and border of the label. Additionally, JLabel
supports tool tips and mnemonics, which can be used to provide additional information about the label or to activate it using the keyboard.
Commonly used Constructors:
JLabel
provides several constructors that can be used to create a label with different types of content. Here are some commonly used constructors:
JLabel()
: Creates a label with no text or icon.JLabel(String text)
: Creates a label with the specified text.JLabel(Icon icon)
: Creates a label with the specified icon.JLabel(String text, Icon icon, int horizontalAlignment)
: Creates a label with the specified text, icon, and horizontal alignment.
Here’s an example of using the JLabel(String text)
constructor to create a label with the text “Hello, World!”:
JLabel label = new JLabel("Hello, World!");
And here’s an example of using the JLabel(Icon icon)
constructor to create a label with an icon:
Icon icon = new ImageIcon("path/to/image.png"); JLabel label = new JLabel(icon);
The JLabel(String text, Icon icon, int horizontalAlignment)
constructor can be used to create a label with both text and an icon, as well as to specify the horizontal alignment of the contents. For example, to create a label with the text “Hello” and an icon aligned to the right, you could use:
Icon icon = new ImageIcon("path/to/image.png"); JLabel label = new JLabel("Hello", icon, SwingConstants.RIGHT);
Note that SwingConstants.RIGHT
is used to specify the right alignment for the icon.
Commonly used Methods:
JLabel
provides several methods that can be used to customize and interact with the label. Here are some commonly used methods:
setText(String text)
: Sets the text displayed by the label to the specified string.setIcon(Icon icon)
: Sets the icon displayed by the label to the specified icon.setHorizontalAlignment(int alignment)
: Sets the horizontal alignment of the label’s contents. TheSwingConstants
interface provides constants for left, center, and right alignment.setVerticalAlignment(int alignment)
: Sets the vertical alignment of the label’s contents. TheSwingConstants
interface provides constants for top, center, and bottom alignment.setToolTipText(String text)
: Sets the tool tip text for the label.setBorder(Border border)
: Sets the border of the label.getText()
: Returns the text currently displayed by the label.getIcon()
: Returns the icon currently displayed by the label.getHorizontalAlignment()
: Returns the horizontal alignment of the label’s contents.getVerticalAlignment()
: Returns the vertical alignment of the label’s contents.getToolTipText()
: Returns the tool tip text for the label.getBorder()
: Returns the border of the label.
Here’s an example of using some of these methods to customize a label:
JLabel label = new JLabel("Hello, World!"); label.setIcon(new ImageIcon("path/to/image.png")); label.setHorizontalAlignment(SwingConstants.CENTER); label.setToolTipText("This is a tooltip"); label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
In this example, we create a label with the text “Hello, World!” and then use the setIcon()
method to set the label’s icon, the setHorizontalAlignment()
method to center the contents, the setToolTipText()
method to set the tool tip text, and the setBorder()
method to set the label’s border.
Java JLabel Example:
Sure! Here’s an example of using JLabel
to create a simple GUI application with a label that displays an image:
import javax.swing.*; import java.awt.*; public class JLabelExample extends JFrame { private JLabel label; public JLabelExample() { setTitle("JLabel Example"); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ImageIcon icon = new ImageIcon("path/to/image.png"); label = new JLabel(icon); label.setHorizontalAlignment(SwingConstants.CENTER); label.setVerticalAlignment(SwingConstants.CENTER); add(label, BorderLayout.CENTER); } public static void main(String[] args) { JLabelExample example = new JLabelExample(); example.setVisible(true); } }
In this example, we create a JFrame
window with the title “JLabel Example” and a size of 400×300 pixels. We then create an ImageIcon
object from an image file and use it to create a JLabel
with the image as its icon. We center the label’s contents horizontally and vertically using the setHorizontalAlignment()
and setVerticalAlignment()
methods. Finally, we add the label to the center of the window using the add()
method with the BorderLayout.CENTER
constraint.
When we run the program, it will display the image in the center of the window. You can replace the image file path with the path to an image file on your own computer to display a different image.
Java JLabel Example with ActionListener:
Sure! Here’s an example of using JLabel
with an ActionListener
to create a simple GUI application that updates the label’s text when a button is clicked:
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JLabelActionListenerExample extends JFrame implements ActionListener { private JLabel label; public JLabelActionListenerExample() { setTitle("JLabel ActionListener Example"); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); label = new JLabel("Hello, World!"); label.setHorizontalAlignment(SwingConstants.CENTER); label.setVerticalAlignment(SwingConstants.CENTER); JButton button = new JButton("Click me!"); button.addActionListener(this); JPanel panel = new JPanel(); panel.add(button); add(label, BorderLayout.CENTER); add(panel, BorderLayout.SOUTH); } public void actionPerformed(ActionEvent e) { label.setText("Button clicked!"); } public static void main(String[] args) { JLabelActionListenerExample example = new JLabelActionListenerExample(); example.setVisible(true); } }
In this example, we create a JFrame
window with the title “JLabel ActionListener Example” and a size of 400×300 pixels. We then create a JLabel
with the text “Hello, World!” and center it horizontally and vertically using the setHorizontalAlignment()
and setVerticalAlignment()
methods. We also create a JButton
with the text “Click me!” and add an ActionListener
to it using the addActionListener()
method. When the button is clicked, the actionPerformed()
method is called and updates the label’s text to “Button clicked!”. Finally, we add the label to the center of the window and the button to a JPanel
in the south region of the window.
When we run the program, it will display the label with the text “Hello, World!” and a button with the text “Click me!”. When we click the button, the label’s text will be updated to “Button clicked!”.