In this Java Swing tutorial, you will learn Java GUI basics like-

What is Swing in Java? What is a Container Class? What is GUI in Java? How to Make a GUI in Java with Example Java Layout Manager Java BorderLayout Java FlowLayout Java GridBagLayout

Java Swing class Hierarchy Diagram

All components in Java Swing are JComponent which can be added to container classes.

What is a Container Class?

Container classes are classes that can have other components on it. So for creating a Java Swing GUI, we need at least one container object. There are 3 types of Java Swing containers.

Panel: It is a pure container and is not a window in itself. The sole purpose of a Panel is to organize the components on to a window.

Frame: It is a fully functioning window with its title and icons.

Dialog: It can be thought of like a pop-up window that pops out when a message has to be displayed. It is not a fully functioning window like the Frame.

How to Make a GUI in Java with Example

Now in this Java GUI Tutorial, let’s understand how to create a GUI in Java with Swings in Java examples. Step 1) Copy code into an editor In first step Copy the following code into an editor. Step 2) Run the code Next step, Save, Compile, and Run the code Step 3) Copy following code into an editor Now let’s Add a Button to our frame. Copy following code into an editor from given Java UI Example

Step 5) Add two buttons How about adding two buttons? Copy the following code into an editor. Step 6) Save & Run the program Next, Save, Compile, and Run the program. Step 7) Check output Unexpected output =? Buttons are getting overlapped.

Java Layout Manager

The Layout manager is used to layout (or arrange) the GUI Java components inside a container. There are many layout managers, but the most frequently used are-

Java BorderLayout

A BorderLayout places components in up to five areas: top, bottom, left, right, and center. It is the default layout manager for every java JFrame

Java FlowLayout

Java GridBagLayout

It is the more sophisticated of all layouts. It aligns components by placing them within a grid of cells, allowing components to span more than one cell.

Step 8) Create chat frame How about creating a chat frame like below?

Try to code yourself before looking at the program below.