How to set JFrame Size according to the Screen

Hello guys! In this tutorial, our focus is on how to set JFrame Size according to the device screen resolution.

Setting JFrame Size according to the Screen

We first create a Java JFrame using the Java Swing library. As a result, our goal now is to set the JFrame size perfectly to the screen resolution size of our device. There are however two methods/approaches in achieving this. We will particularly discuss each one as we move on.

Approach1 – Using getScreenSize() method

Firstly, we use getScreenSize() method of the Java Toolkit class to get the resolution of the screen in dots per inch. Secondly, we use the frame.setSize() to set the dimensions that we receive from the getScreenSize() method. This method contains width as the first parameter and height as the second parameter.

Furthermore, The height and width properties of the getScreenSize() method allow us to get the required information about the height and width of our device screen size in pixels. As a result, we use this to set the dimensions. The main use of the setSize() component is to set the layout of the JFrame.

Let us see the code:

//APPROACH - 1 : Using getScreenSize() method
Toolkit tk=Toolkit.getDefaultToolkit(); //Initializing the Toolkit class.
Dimension screenSize = tk.getScreenSize(); //Get the Screen resolution of our device.
f.setSize(screenSize.width,screenSize.height); //Set the width and height of the JFrame.

Output:

How to set JFrame Size according to the Screen

Approach2 – Using MAXIMIZED_BOTH:

We use the JFrame MAXIMIZED_BOTH field to maximize the frame in both directions. It means that we maximize the JFrame in the vertical and horizontal directions. This field is used as a convenience instead of using the JFrame MAXIMIZED_VERT and MAXIMIZED_HORIZ to maximize the vertical directions and the horizontal directions respectively.

This MAXIMIZED_BOTH has an inner implementation represented as MAXIMIZED_VERT | MAXIMIZED_HORIZ. This makes it easy to write in any application as a cause of convenience.

Let’s see the code:

//APPROACH - 2 : Using MAXIMIZED_BOTH
//Maximize both the horizontal and vertical directions
f.setExtendedState(JFrame.MAXIMIZED_BOTH);

Output:

How to set JFrame Size according to the Screen

Analysis:

Therefore, we can observe that the output is the same in both cases. As a result, we have accomplished our tasks of setting the JFrame Size according to our device screen resolution. Lastly, here is the entire code for an improved understanding of the concepts we have learned.

Let’s see the code:

//Java Program to set JFrame Size according to the Screen
import javax.swing.*;
import java.awt.*;

public class SetScreenSize extends JFrame {
    public static void main(String[] args) {

        // Creating a Java JFrame
        JFrame f = new JFrame();        
        f.setTitle("Set Size According to Screen"); 
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        f.setVisible(true);

        //APPROACH - 1 : Using getScreenSize() method
        Toolkit tk=Toolkit.getDefaultToolkit(); //Initializing the Toolkit class.
        Dimension screenSize = tk.getScreenSize(); //Get the Screen resolution of our device.
        f.setSize(screenSize.width,screenSize.height); //Set the width and height of the JFrame.

        //APPROACH - 2 : Using MAXIMIZED_BOTH
        f.setExtendedState(JFrame.MAXIMIZED_BOTH); //Maximize both the horizontal and vertical directions
       
    }
}

I hope that this post was useful. Thanks for reading!

Leave a Reply

Your email address will not be published.