Create Hello World Java Swing

Create Hello World Java Swing

This is simple code to create Hello World using Java Swing. Please have sample code below:

  • HelloWorldSwring.java:
package com.javahonk;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class HelloWorldSwing {

	public static void main(String[] args) {

		JFrame frame = new JFrame("Swing Hello World");
		frame.setMinimumSize(new Dimension(500, 400));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JLabel myLabel = new JLabel("Swing Hello World !!!", SwingConstants.CENTER);
		myLabel.setFont(new Font("Serif", Font.ITALIC, 24));
		myLabel.setBackground(Color.green);
		myLabel.setOpaque(true);
		frame.getContentPane().add(myLabel, BorderLayout.NORTH);
		myLabel.setPreferredSize(new Dimension(120, 100));
		
		frame.setVisible(true);

	}
}
  •  Output:

Create Hello World Java Swing

  • For more details please visit oracle tutorial here

Leave a Reply

Your email address will not be published. Required fields are marked *