Skip to content
Sign UpLog In
This post is read-only. Explore Repls and connect with other creators on Community.View Community
The info in this post might be out of date, check out our docs instead. View docs
21

Java Swing Tutorial Day 1. LEARN HOW TO CREATE A GUI!

EpicGamer007
EpicGamer007

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.

Swing!

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:

IT IS PORTABLE

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 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // IMPORTANT. TYPE THIS IN EVERY FRAME OR ELSE THE CLOSE BUTTON WILL NOT DO ANYTHING 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 */ frame.getContentPane().add(button); //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" frame.setLocationRelativeTo(null); // Optional. Sets the frame in the center of the screen frame.setVisible(true); // 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!

Edit:

Day 2 is out!

2 years ago

Voters

Comments

TopNew
5
Jakman
Jakman

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

2 years ago
2
EpicGamer007
EpicGamer007

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

2 years ago
1
Jakman
Jakman

@AbhayBhat
sure. I have time

2 years ago
1
acohenwolcott
acohenwolcott

Thanks for this tutorial. Very helpful for getting started.

2 years ago
1
YesiHernandez
YesiHernandez

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

2 years ago
1
EpicGamer007
EpicGamer007

@YesiHernandez
con qué necesitas ayudar?

2 years ago
1
YesiHernandez
YesiHernandez

@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

2 years ago
1
EpicGamer007
EpicGamer007

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

2 years ago
1
JorgeMejia2
JorgeMejia2

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.*;

2 years ago
1
EpicGamer007
EpicGamer007

@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!

2 years ago
1
EpicGamer007
EpicGamer007

@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!

2 years ago
Load more