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.
In this JFrame in Java Tutorial for beginners, we will see how to create JFrame in Java and how to use JFrame using its associated methods.
Also Read : Login form in Java Swing
So let’s begin JFrame in Java Tutorial,
JFrame in Java Tutorial for Beginners
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 see how to create JFrame in Java and then how to use JFrame in Java step by step.
How to Create JFrame in Java
Steps For Creating a GUI 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.
1 2 3 4 5 6 7 8 |
import javax.swing.*; public class myWindow { public static void main(String args[] ) { JFrame f= new JFrame();// creating a Frame } } |
- 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 “.
1 2 3 4 5 6 7 8 9 |
import javax.swing.*; public class myWindow { public static void main(String args[] ) { JFrame f= new JFrame(); f.setVisible(true);//setting visibility } } |
- Save it, compile and run the program. Now you can see the window is visible.
Output :
- 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. “
1 2 3 4 5 6 7 8 9 10 |
import javax.swing.*; public class myWindow { public static void main(String args[] ) { JFrame f= new JFrame(); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//specifying close operation } } |
- 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) .
1 2 3 4 5 6 7 8 9 10 11 |
import javax.swing.*; public class myWindow { public static void main(String args[] ) { JFrame f= new JFrame(); f.setVisible(true); f.setSize(100,200);//setting size of the window f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 |
import javax.swing.*; public class myWindow { public static void main(String args[] ) { JFrame f= new JFrame(); f.setVisible(true); f.setSize(100,200); f.setLocation(200,200);//setting location of the window f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
- 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).
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import javax.swing.*; public class myWindow { public static void main(String args[] ) { JFrame f= new JFrame(); f.setVisible(true); //f.setSize(100,200); //f.setLocation(200,200); f.setBounds(200,200,100,200);//setting size and location of the window simultaneously f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
import javax.swing.*; public class myWindow { public static void main(String args[] ) { JFrame f= new JFrame(); f.setTitle("myWindow");//setting title of the window f.setVisible(true); f.setSize(300,200); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.awt.*; import javax.swing.*; public class myWindow { public static void main(String args[] ) { JFrame f= new JFrame(); f.setTitle("myWindow"); f.setVisible(true); f.setSize(300,200); Container c= f.getContentPane();//creating container object c.setBackground(Color.green);//calling setBackground method f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
- 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. “
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.awt.*; import javax.swing.*; public class myWindow { public static void main(String args[] ) { JFrame f= new JFrame(); f.setTitle("myWindow"); f.setVisible(true); f.setSize(300,200); Container c= f.getContentPane(); c.setBackground(Color.green); f.setResizable(false);//restricting the user to resize the window f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import javax.swing.*; import java.awt.*; class myWindow{ public static void main(String[] args){ JFrame f= new JFrame(); f.setTitle("myWindow"); f.setSize(640,480); Container c= f.getContentPane(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ImageIcon icon =new ImageIcon("flower.jpg"); JLabel label=new JLabel(icon); label.setBounds(0,0,icon.getIconWidth(),icon.getIconHeight()); f.add(label); f.setVisible(true); } } |
- 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 in which we have learned how to create a GUI JFrame in Java and how to use that. 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…
- How to Play Mp3 File in Java Tutorial | Simple Steps
- Tic Tac Toe Java Code Against Computer With Source Code
- Calculator Program in Java Swing/JFrame with Source Code
- Registration Form in Java With Database Connectivity
- How to Create Login Form in Java Swing
- Text to Speech in Java
- How to Create Splash Screen in Java
- Java Button Click Event
- Menu Driven Program in Java
Finally I’ve found someone who explain too easy, thanks
Super easy
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 ?
Thank you so much for helping me.
You are welcome
I appreciate that
thanks Bro…
Thanks, it is good explanation
wonderful presentation indeed
HI,
This is perfectly fine and is in very simple to learn and understand step by step.
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 ?
Thank you so much! I really appreciate your explanation from scratch and covering the entire concept.
Thank you so much for your appreciation