In This post we will show you and example of Hello World using Java Swing . We will configures a JFrame and attach a JLabel to display a “hello world” centered in the JLabel component.

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

public class SwingHelloWorld {

    public static void main(String[] args) {

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

        // 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);

    }
}

The output :

Also read : Java Basics

Tagged:

Laisser un commentaire