Saturday, September 13, 2008

Java 11: Swing GUI Intro



Here is an example of Hello World program using Swing. Please understand that it is NOT a part of the above video clip. It is provided for education purpose only.

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

public class HelloWorld {

public static void main(final String[] args) {
// Create frame with title "Hello, World!"
JFrame frame = new JFrame("Hello, World!");

//This stops the app on window close.
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

// add a label in the frame, same as frame.getContentPane().add
frame.add(new JLabel("Hello, World!"));

// make sure Size of the frame is set according to its content
frame.pack();

// set the frame visible
frame.setVisible(true);
}
}