Java Swing Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.00

SwingHello.java - My First Swing Program

This section provides a tutorial example on how to a simple Java Swing program. SwingHello.java is my first Swing program that display a blank window with 'Hello world!' in the window title bar.

Below is my first Swing program, SingHello.java:

import javax.swing.*;
public class SwingHello {
   public static void main(String[] a) {
      JFrame f = new JFrame("Hello world!");
      f.setVisible(true);
   }
}

If you have JDK installed on your system, you can compile and run this program in a command window:

javac SwingHello.java
java -cp . SwingHello

Once the program is running, you will see a small new window showing up on your screen. If use the mouse pointer to drag its edges to make it larger, you can see the hello text in the title area as shown in this picture:
First Swing - Hello

The new window will stay on your screen until you press Ctrl-C in the command window to terminate the running program.

If you don't have JDK installed on your system, you can read my other book, "Herong's Notes on Java", on how to install JDK.

Sample programs listed in this section have been tested with JDK 1.3.1, 1.4.1, 1.5.0 and 1.6.0.

Last update: 2009.

Sections in This Chapter

What Is What Is Swing?

SwingHello.java - My First Swing Program

Dr. Herong Yang, updated in 2009
SwingHello.java - My First Swing Program