Main Tutorials

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

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments