CJCoding With Joseph
15free / daySign inCreate account

Mastering Java Swing

JLabel

Basic Java Swing Elements

A JLabel displays static text or images in your GUI and provides information to users without interactivity.

import javax.swing.*;

public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame("JLabel Example");
        JPanel panel = new JPanel();
        JLabel label = new JLabel("Welcome to Java Swing!"); // Create a label

        panel.add(label); // Add label to the panel
        frame.add(panel); // Add panel to the frame

        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
Members only

Keep reading this lesson

You've reached the end of the free preview. Unlock the rest of this lesson plus 27 more lessons in Mastering Java Swing.

  • Unlimited coding questions
  • Unlimited access to all classroom material
  • Weekly live call for questions and guidance
  • 3,000 AI tutor tokens / month
  • Personal SQL database (3 MB)
  • Progress tracking

Cancel any time.