Java Swing Hello World example

The below program launch a Java Swing application; It configures a JFrame and attach a JLabel to display a hello world and center the JLabel component.

Read the comment for self-explanatory.

SwingHelloWorld.java

package com.mkyong.swing

import javax.swing.*;
import java.awt.*;

public class SwingHelloWorld {

    public static void main(String[] args) {

        JFrame frame = new JFrame("Hello World Java Swing");

        // set frame site
        frame.setMinimumSize(new Dimension(800, 600));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // center the JLabel
        JLabel lblText = new JLabel("Hello World!", SwingConstants.CENTER);

        // add JLabel to JFrame
        frame.getContentPane().add(lblText);

        // display it
        frame.pack();
        frame.setVisible(true);

    }
}

Output

Java Swing hello world output

References

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments