In this article, we will learn how to create a simple Swing hello world program. We also learn how to compile and run a Swing application from the command line.
Let's get started!.
Here are the steps you need to follow:
- Install the latest release of the Java SE platform
- Create a program that uses Swing components
- Compile the program
- Run the program
- Output
1. Install the Latest Release of the Java SE Platform
Install the latest release of the Java SE platform, if you haven't already done so. You can download the latest release of the JDK for free from http://www.oracle.com/technetwork/java/javase/downloads/index.html.
2. Create a Program That Uses Swing Components
Let's create a simple Swing program called HelloWorldSwing, that brings up the GUI shown in Output section.
First, create a directory and name it as "swing-examples". Now create a Java class named HelloWorldSwing.java and add the following code to it:
package net.javaguides.javaswing.examples;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.Border;
/**
*
* This class demonstrates the simple hello world swing program.
* @Ramesh Fadatare
*/
public class HelloWorldSwing {
private static void createAndShowGUI() {
JFrame jFrame = new JFrame("Hello World Swing Example");
jFrame.setLayout(new FlowLayout());
jFrame.setSize(500, 360);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Hello World Swing");
Border border = BorderFactory.createLineBorder(Color.BLACK);
label.setBorder(border);
label.setPreferredSize(new Dimension(150, 100));
label.setText("Hello World Swing");
label.setHorizontalAlignment(JLabel.CENTER);
label.setVerticalAlignment(JLabel.CENTER);
jFrame.add(label);
jFrame.setVisible(true);
}
public static void main(String[] args) {
createAndShowGUI();
}
}
Let's understand the above Swing hello world program.
Creating a JFrame window
From the above program, we created a JFrame just like any other Java objects:
JFrame jFrame = new JFrame("Hello World Swing Example");
Setting the layout manager
The default layout of the frame is BorderLayout, here we are setting FlowLayout layout like this:
jFrame.setLayout(new FlowLayout());
Adding child components
We are adding a label to JFrame like this:
JLabel label = new JLabel("Hello World Swing");
Border border = BorderFactory.createLineBorder(Color.BLACK);
label.setBorder(border);
label.setPreferredSize(new Dimension(150, 100));
label.setText("Hello World Swing");
label.setHorizontalAlignment(JLabel.CENTER);
label.setVerticalAlignment(JLabel.CENTER);
jFrame.add(label);
Specifying window closing behavior
We can specify which action will be executed when the user clicks on the frame’s close button. Here we are setting up exit the program (JVM terminates) after the window closes:
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Showing the frame on the screen
Make sure we set size for the frame before making it visible:
jFrame.setSize(500, 360);
jFrame.setVisible(true);
3. Compile the Program
Your next step is to compile the program. To compile the example, from the directory "swing-examples" the HelloWorldSwing.java file:
javac swing-examples/HelloWorldSwing.java
If you prefer, you may compile the example from within the "swing-examples" directory:
javac HelloWorldSwing.java
4. Run the Program
After you compile the program successfully, you can run it. From the directory above the "swing-examples" directory:
java swing-examplesHelloWorldSwing
If you prefer, you may run the example from within the "swing-examples" directory:
javac HelloWorldSwing.java
5. Output
Here is the output of this Swing hello world program:
Related Swing Examples
- Java Swing Exit Button - In this post, I show you how to exit a Swing application when clicking on the exit button.
- Swing ToolTip Tutorial with Example - In this tutorial, we will learn how to add tooltip text to a Swing component.
- Java Swing BorderLayout Example - In this example, we will learn how to use BorderLayout in GUI/swing based applications.
- Java Swing GridLayout Tutorial with Examples - In this tutorial, we will learn how to use GridLayout in GUI/swing based applications.
- Swing Mouse Move Events using MouseMotionAdapter - In this tutorial, we will learn how to receive mouse motion events using MouseMotionAdapter.
- Java Swing CheckBox Example - In this post, I show you how to use JCheckBox class to create a Radio button in a Swing-based application.
- Java Swing Radio Button Example - In this post, I show you how to use JRadioButton class to create a Radio button in a Swing-based application.
- Java Swing Progress Bar Example - In this post, I show you how to create a progress bar using the JProgressBar component in swing-based applications.
- Java Swing Combo Box Example - In this post, I show you how to create a combo box using a JComboBox component in swing-based applications.
- Java Swing Slider Example - In this post, I show you how to create a slider using the JSlider component in swing-based applications.
- Java Swing Toggle Button Example - In this post, we will learn how to create a toggle button using the JToggleButton component in swing-based applications.
Comments
Post a Comment
Leave Comment