JFrame Tutorial in Java with Examples | Java Swing

Hello Friends, Welcome to my new post of Java JFrame Tutorial for Beginners. In my last post we have seen Java Environment Setup in Windows and if you don’t know the basic idea of how to run a Java program then go through my previous post.

Since we are dealing with advanced topics, you should have a basic knowledge of Java programming. I’m here to teach you about advanced topics, and in this JFrame in Java Tutorial, we will make a simple Window using the JFrame class along with its associated methods.

Also Read : Login form in Java Swing

So let’s begin JFrame in Java Tutorial,

JFrame in Java Tutorial

What is JFrame in Java?

  • It is an extended version of Java.awt.Frame that adds support for the JFC/Swing architecture.
  • JFrame is the core class of javax.swing package and is used to develop GUI (Graphical User Interface) in which various visual objects like Text Field, Radio Button, Scroll Bar, Check Box etc are embedded. This GUI is called Window pane.
  • JFrame is a window with a title Bar, Border, a resizable Border, menu Bar, Buttons that close or iconify the window and a content pane that acts as a container.
  • JFrame contains a JRootpane, which is where all of the components are added, and this JRootpane is shared among all container objects. When components are added to a JFrame, they are added to the content pane(JRootpane).
  • The JFrame is typically the outermost container used in GUI applications.

So this was all about JFrame in Java, and now we will create our first window using JFrame in Java Step by Step.

Creating First Window Using JFrame in Java

Steps For Creating a JFrame in Java

  • Import javax.swing.JFrame package, or you can also use javax.swing.* package. This package will make available the JFrame class.
  • Create a class and give a name to it.
  • Write main method and inside the main method create an object of the JFrame class.
  • Now save the program, compile it, and run it.
  • The most exciting thing about this program is that although you have successfully compiled and run the program, nothing shows up. It is just because the Frame is invisible. To make it visible, we have to call the setVisible() method of the JFrame class using the JFrame class object.

Setting the visibility of JFrame in Java

setVisible() method

  • This method is used to control whether a component will be displayed on the screen or not.
  • It takes a Boolean value as an argument.
  • setVisible(false) –“it hides the component by making it invisible “.
  • setVisible(true) –“it reveals the component by making it visible “.
  • Save it, compile and run the program. Now you can see the window is visible.

Output :

JFrame tutorial for beginners
JFrame tutorial for beginners- fig-1
  • Now, as you can see, the window is visible, and by default, it opens up in the top-left corner of the screen.
  • There is an interesting thing associated with the above program. Although the close button has been clicked, the program will not be removed from the memory because of its TSR(Terminate but stay resident) characteristics. However, Something is missing for the complete termination of the program. The solution is to invoke the setDefaultCloseOperation() method.

Setting the Default Close Operation of JFrame/How to Close a JFame in Java

setDefaultCloseOperation() method

  • This method is called when one clicks the Close Button.
  • This method specifies many options for the close Button. We can choose any one of the given constants to determine the close Button’s nature when someone clicks on it. Constants are given below :
  • JFrame.DO_NOTHING_ON_CLOSE – “As the name suggests, it neglects the click, and nothing happens when someone clicks on the Close Button. “
  • JFrame.HIDE_ON_CLOSE –” As the name suggests, it only hides the frame when someone clicks on the Close Button, but the application still remains on the running state. “
  • JFrame.DISPOSE_ON_CLOSE –“As the name suggests, it disposes the frame when someone clicks on the Close Button, but the application still remains on the running state. “
  • JFrame.EXIT_ON_CLOSE –“As the name suggests, it exits the application when someone clicks on the Close Button and removes the program from memory permanently. “
  • So we have learned how to close a JFrame when we click on the exit button.

Setting The Size Of Java JFrame

setSize() method

  • As the name suggests this method is used to set the size of the window displayed on the screen .
  • By default the size is passed to the setSize() method is ” 0 pixel x 0 pixel “.
  • it takes two integer type arguments. first argument  specifies the width of the window and second argument specifies the height of the window . example – setSize(100,200) .
Output :

JFrame tutorial for beginners
Java JFrame tutorial for beginners- fig-2
  • As we can see the output of the above program in above image and  size of the window has been changed.

Setting the Location of JFrame in Java

setLocation() method

  • As the name suggests, this method is used to set the window’s location on the screen.
  • The default location of the window is the top-left corner of the screen.
  • It takes two integer type arguments. The first argument specifies the window’s location on the x-axis, and the second argument specifies the window’s location on the y-axis.
Output :

JFrame tutorial for beginners
Java JFrame tutorial for beginners- fig-3
  • As we can see, the above program’s output in the above image in which the window’s location has been changed in the x-axis and y-axis.

Setting the Size and Location of  The Java JFrame Simultaneously

setBounds() method

  • This method is used to set the size and position(Location) of the window simultaneously.
  • It takes four integer type arguments in which the first two arguments specify the window’s position on the x-axis and y-axis, and the last two arguments specify the size of the window in width and height.                         example –setBounds(200,200,100,200).

Setting The Title Of JFrame in Java

setTitle() method

  • As the name suggests, this method is used to set the title of the window.
  • It takes the string/name of the window to be displayed on the screen as its argument.
Output :

Java JFrame tutorial for beginners- fig-4
  • As we can see in the above image, the Title of the window has been created.

Setting Background Color of JFrame in Java

setBackground() method

  • For this purpose, we have to call the setBackground() method through the object of JFrame. The parameter to be sent is the various static fields of the Color class

Steps to Add Color

  • Import the java.awt.* package. This package makes the setBackground() method available.
  • Since the Background Color can only be added to the container, we have to make a container object for the window f.
  • This is done by calling the getContentPane() method through f.
  • Now we have to call the setBackground() method using the container’s object, which takes various static pre-defined fields of Color class.
Output :

Java JFrame tutorial for beginners- fig-5
  • As we can see in the above image, the background color of the window has been changed.

Restricting The User To Resize The JFrame in Java

setResizable() method

  • For this purpose, we call the setResizeable() method of the JFrame class.
  • This method takes a boolean value as its parameter.
  • setResizable(true) – “allows the user to resize the window. “
  • setResizable(false)- “restricts the user to resize the window. “
Output :

Java JFrame tutorial for beginners- fig-6
  • As we can see in the above image that it restricts the user to resize the window.

How to add Background Image to JFrame in Java

  • This section will learn how to set an image as background in Java JFrame.
  • To set an image as background in JFrame, we will create the object of ImageIcon class, and in its constructor, we have to pass the full path of the image/icon with its correct extension.
  • If the image is in the same folder we have created the program, then we have to give only the image name and its extension otherwise if the image is in the other folder, then we have to provide the whole path of the picture.
  • Then we will create an object of JLabel class, and in its constructor, we will pass the object of ImageIcon class as an argument.
  • Then we can set the location and size of the Image/icon using the method setBounds() .
  • The interesting thing is that if we don’t know the actual width and height of the image and it does not perfectly fit in the label, then we can use the two methods of icon getIconWidth() and getIconHeight() as arguments in setBounds() method.
  • The next thing we have to do is add the label to the frame’s content pane.
  • The code is given below.
Output :

JFrame in Java Tutorial – fig – 7
  • As shown in the above image, we have successfully added a background image to JFrame in Java.

So Friends, Now I’m wrapping up this Java JFrame in Java Tutorial . Please do comment if you are having any doubts or queries regarding this post. For more tutorials about Java and Java Swing, stay tuned with Tutorials Field. Thank You.

People Are Also Reading…

12 thoughts on “JFrame Tutorial in Java with Examples | Java Swing”

  1. Thank you so much for making a tutorial that actually speaks English! Most tutorials move too fast and assume you know something already. This was super helpful ?

    Reply
  2. This is too early but i can say, You are the best. wooooooooooooooow, My java Teacher has made java lessons cut and past for my class and the student are also doing chew and pour. those of us who can’t do the chew and pour are left behind. I always said there’s no hope in my coding journey? but you have proven that there’s hope ahead. i never knew i could understand “GUI” tutorials. Please help me with a full java tutorial to learn. Is there any link ?

    Reply

Leave a Comment