JLabel Icons - potatoscript/JavaSwing GitHub Wiki
A JLabel is a GUI component in Java Swing used for displaying text, images, or both. It is non-editable and mainly used for UI labeling.
✔️ Displays text, images, or both
✔️ Supports custom fonts, colors, and alignment
✔️ Works well in layouts like JPanel
, JFrame
import javax.swing.*;
public class JLabelExample {
public static void main(String[] args) {
// Create JFrame
JFrame frame = new JFrame("JLabel Example");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create JLabel
JLabel label = new JLabel("Hello, Swing!");
// Add label to frame
frame.add(label);
// Make frame visible
frame.setVisible(true);
}
}
✔️ JLabel label = new JLabel("Hello, Swing!");
→ Creates a label with text.
✔️ frame.add(label);
→ Adds the label to the frame.
✔️ By default, text aligns to the left in the container.
import javax.swing.*;
public class JLabelIconExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JLabel with Icon");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Load image
ImageIcon icon = new ImageIcon("icon.png"); // Change to your image path
// Create JLabel with icon
JLabel label = new JLabel(icon);
frame.add(label);
frame.setVisible(true);
}
}
✔️ new ImageIcon("icon.png")
→ Loads an image file.
✔️ JLabel label = new JLabel(icon);
→ Creates a label with the image.
✔️ If the image is not found, no error occurs, but the label will be empty.
📌 Make sure the image file exists in the project folder!
import javax.swing.*;
public class JLabelTextIconExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Text & Icon Label");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Load image
ImageIcon icon = new ImageIcon("icon.png");
// Create JLabel with text & icon
JLabel label = new JLabel("Hello!", icon, JLabel.CENTER);
frame.add(label);
frame.setVisible(true);
}
}
✔️ JLabel(label, icon, JLabel.CENTER)
→ Places text & icon together.
✔️ By default, text is to the right of the icon.
label.setHorizontalTextPosition(JLabel.LEFT); // Text on the left
label.setHorizontalTextPosition(JLabel.RIGHT); // Text on the right
label.setVerticalTextPosition(JLabel.TOP); // Text above icon
label.setVerticalTextPosition(JLabel.BOTTOM); // Text below icon
✔️ setHorizontalTextPosition(JLabel.RIGHT);
→ Moves text relative to the icon.
✔️ setVerticalTextPosition(JLabel.BOTTOM);
→ Moves text above or below the icon.
import javax.swing.*;
import java.awt.*;
public class StyledLabelExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Styled JLabel");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Styled Text");
label.setFont(new Font("Arial", Font.BOLD, 18)); // Set font
label.setForeground(Color.BLUE); // Set text color
frame.add(label);
frame.setVisible(true);
}
}
✔️ setFont(new Font("Arial", Font.BOLD, 18));
→ Changes font size & style.
✔️ setForeground(Color.BLUE);
→ Changes text color.
JLabel label = new JLabel("<html>Hello<br>Welcome to Swing!</html>");
✔️ Use <html>...</html>
tags to add multi-line text inside a JLabel
.
label.setHorizontalAlignment(JLabel.CENTER);
✔️ Centers the label within its container.
✅ JLabel
is used to display text & images.
✅ Supports icons, colors, fonts, and alignment.
✅ Use HTML to create multi-line labels.
✅ Combine text & icons with JLabel
.