Java Swing Tutorials - Herong's Tutorial Examples - v4.32, by Herong Yang
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!"); f.setVisible(true); } }
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:
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:
Table of Contents
►Introduction of Java Swing Package
►SwingHello.java - My First Swing Program
Graphics Environment of the Local System
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
JEditorPane - The Editor Pane Class
SwingWorker - The Background Task Worker
AWT (Abstract Windows Toolkit)