📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
- 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
2. Create a Program That Uses Swing Components
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();
}
}
Creating a JFrame window
JFrame jFrame = new JFrame("Hello World Swing Example");
Setting the layout manager
jFrame.setLayout(new FlowLayout());
Adding child components
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
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Showing the frame on the screen
jFrame.setSize(500, 360);
jFrame.setVisible(true);
3. Compile the Program
javac swing-examples/HelloWorldSwing.java
javac HelloWorldSwing.java
4. Run the Program
java swing-examplesHelloWorldSwing
javac HelloWorldSwing.java
5. Output
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