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 displays 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 to 1.8.0.

Last update: 2014.

Table of Contents

 About This Book

Introduction of Java Swing Package

 What Is Swing?

SwingHello.java - My First Swing Program

 Graphics Environment of the Local System

 JFrame - Main Frame Class

 JLabel - Swing Label Class

 JButton - Swing Button Class

 JRadioButton - Swing Radio Button Class

 JTextField - Swing Text Field Class

 Menu Bar, Menus, Menu Items and Listeners

 Creating Internal Frames inside the Main Frame

 Layout of Components in a Container

 LookAndFeel and UIManager

 Option Dialog Boxes

 JEditorPane - The Editor Pane Class

 SwingWorker - The Background Task Worker

 References

 PDF Printing Version