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, SwingHello.java:

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

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

herong> javac SwingHello.java
herong> java 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 (on Windows) - Hello World!
First Swing (on Windows) - Hello World!

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

If you are using a macOS computer, the Java Swing window will look differently:

First Swing (on macOS) - Hello World!
First Swing (on macOS) - Hello World!

Table of Contents

 About This Book

 JDK (Java Development Kit)

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

 JCheckBox - Swing Check Box Class

 JRadioButton - Swing Radio Button Class

 JTextField - Swing Text Field Class

 JComboBox - Swing Combo Box 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

 AWT (Abstract Windows Toolkit)

 Integration with Desktop System

 Archived Tutorials


 Full Version in PDF/EPUB