What is JFrame in Java with Examples

Hello, guys! In this tutorial, we will focus on learning what Java JFrame is with examples.

JFrame in Java with Examples

We will discuss the Java JFrame in a detailed manner in this tutorial. The topics covered in this tutorial are :

  • Introduction to JFrame
  • Creation of a Basic JFrame
  • Methods in JFrame
  • A simple Example

In addition to this tutorial clearing the fundamentals in the JFrame, it will also give you various insights on how to get started. However, once done with the reading, you would be able to write various programs. Now, you can try extending this and implementing it on your own. Moreover, I will provide some useful links at the end for a more detailed understanding.

Introduction to JFrame

The JFrame is a class of java.swing package which is inherited and extended from the Java AWT package. It is used for creating lightweight window applications. It includes various components such as labels, panels, buttons, etc for creating a beautiful Graphical User Interface (GUI).

Above all, every Java Swing application starts by implementing a JFrame. As a result, it is basically a top-level container to create window-based applications.

Creation of a Basic JFrame

There are various ways to create a JFrame in Java. Let’s look at those.

  1. JFrame() – Creates a new frame with no title which is initially invisible.
  2. JFrame(GraphicsConfiguration gc) – Initializes a frame with given Graphics Configuration and no title.
  3. JFrame(String title) – Specifies a new frame with the given title.

Here is a small example that demonstrates this.

//Creation of a JFrame
JFrame frame = new JFrame("JFrame Example"); //Sets the title of the frame.
frame.setVisible(true);  //Displaying the frame

Methods in JFrame

In this section, we will look at various methods of JFrame that are useful while working with window-based applications.

  1. createRootPane() – Creates the default root pane invoked by the constructors.
  2. frameInit() – Initializes the JFrame with memory.
  3. setContentPane(Container contentPane) – Sets the property of the content pane layer.
  4. setIconImage(Image img) – Specifies a single image displayed as icon on window.
  5. getRootPane() – Returns the rootPane object of the frame.

In conclusion, we will now see a simple example using the methods we have discussed.

A simple Example

Firstly, we will create a simple JFrame to understand all the concepts learned so far. Secondly, we will create a JFrame with a panel and add a text and a button to the panel. Finally, add it to the frame and display it. Moreover, for easier understanding, the code is commented at each line.

Let’s see the code:

// Java Program to explain JFrame with an example

// Imports
import javax.swing.*;
public class JFrameExample {  
    public static void main(String[] args) { 
        //Creation of a JFrame
        JFrame f = new JFrame("JFrame Example"); //Sets the title of the frame.
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //Closes JFrame on exit button
        f.setBounds(100, 200, 380, 300); //Sets the location of the frame.
        f.setLocationRelativeTo(null); //Sets the frame to center location
        //Some operation to insert various objects into frame
        JPanel panel = new JPanel(); //Creating a JPanel   
        JLabel label = new JLabel("Welcome to"); //Creating a JLabel
        JButton button = new JButton("CodeSpeedy!"); //Creating a JButton
        panel.add(label);  //Adding the label to the panel
        panel.add(button);  //Adding the button to the panel
        f.add(panel);  //Finally, Adding the panel with text and button to frame

        f.setVisible(true);  //Displaying the final frame with changes


Jframe Example in Java

Thanks for reading this tutorial. Keep exploring, Keep learning!.

Also Read:

Leave a Reply

Your email address will not be published. Required fields are marked *