Learn to Code via Tutorials on Repl.it!

← Back to all posts
Java Swing Tutorial Day 1. LEARN HOW TO CREATE A GUI!
EpicGamer007 (1736)

Java Swing Tutorial Day 1

G'Day fellow replers. After noticing barely any people learned from my first tutorial series, I decided to stop teaching Java OOP. Now I will be teaching something more fun.


javax.swing is a built in package in java which you can get by importing it. Swing is used for making GUI's which are Graphical User Interfaces. This is like pythons TKinter. Both of these help you to create Desktop applications.

But what are some examples of swing being used? Isn't everyone useing javafx now and swing is supposedly really trash?

* IntelliJ: A java IDE which is written using java swing
* NetBeans: Another java IDE which is written using java swing
* Eclipse: YET Another java IDE which is written using java swing
* SQuirrel SQL Client: A place where you can use SQL...
* yEd: A general-purpose diagramming program with a multi-document interface(According to wikipedia) which uses java.swing
* I cannot confirm it but Minecraft might use swing.

And here are some programs I made with swing:

This is just a few examples of wher swing is used. But the best thing about Swing:


If you make a swing application then jar it up(make it into a jar file), Any OS would be able to run it(And this is true for all java applications).

It is also extremely simple is you already know some java.

Yes it really is. It is as simple as...

import javax.swing.*; // The package where the components are
import java.awt.event.*;    // Used for event listeners and events

public class Main{

	public static void main(String[] args){
		JFrame frame = new JFrame("My first SWing GUI");
		// Create a frame object

		frame.setSize(500, 500);
		// Set the size of the frame to 500 pixels in length and 500 pixels in height


		JButton button = new JButton("Click me!");
		//Create an instance of JButton

		button.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e){

				System.out.println("The button was clicked!");




		The button will not do anything(Other than have a pushed look when it is pressed) unless you add this, an actionListener.

		When you press a JButton, the button fires an ActionEvent and the only way to listen to these events it to add an ActionListener which has a method:
		actionPerformed(ActionEvent e)

		which is called when the button is pressed


		//Adds the button to the frame's content pane
		// Note: A frame in swing is just like a photoframe, it is the outside and holds the content. 
		//The Content Pane is where all the items go in or the "Picture in the frame"	
		// Optional. Sets the frame in the center of the screen
		// IMPORTANT. This is to make it visible on the screen.


That's it! That is all you have to do to create a frame with a button that does something!

Type this in(Type, do not copy) to a new Java Swing Project and try it!

I hope y'all that liked this tutorial, enjoyed it, and learned something new. In the next tutorial, I am planning on going over more JComponents or Graphics(TELL ME IN THE COMMENTS WHICH ONE YOU WANT).

Thank you!


Day 2 is out!

Jakman (449)

This is very nice. I like to see more tutorials in languages other than python. This gave a clear understanding of Swing.

EpicGamer007 (1736)

@Jakman, That is great! I will do my best. Also, want to work on the project now?

acohenwolcott (0)

Thanks for this tutorial. Very helpful for getting started.

YesiHernandez (0)

Hola me gustaría que me ayudes con un trabajo para la escuela, tendrás tiempo?

YesiHernandez (0)

@EpicGamer007 Hola buenas noches es que tengo que elaborar 4 proyectos de este tema donde tenga un compenente ir cada proyecto, los que se van a elegir menú,radio button, lista respetable, combo

EpicGamer007 (1736)

@YesiHernandez bien, ¿pero puedes hablar inglés? Es muy difícil para mí hablar español y mi español es muy malo...

JorgeMejia2 (0)

Great tutorial! Thank you. I gave this a try, but for some reason, it would give an error unless I added an import to the event classes as:

import java.awt.event.*;

EpicGamer007 (1736)

@JorgeMejia2 really? oh yea i am sorry, that is my bad, you do have to add that import as java.awt. does not include java.awt.event. and it only includes the classes. thanks for notifying me, i will update this post!

EpicGamer007 (1736)

@ZDev1, @Highwayman, @TheForArkLD, @NDLFOREVER, @Jakman, @DynamicSquid

The next tutorial is out here!

Note: If you do not want me to ping you to give you updates, please reply to this message saying that you don't want to be pinged.

Thank you!