Java Screen Size: Get Width & Height

One option: You can use the Toolkit class to obtain the width and height of the screen. Here is an example code:

import java.awt.Dimension;
import java.awt.Toolkit;

public class Main {
    public static void main(String[] args) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int screenWidth = (int) screenSize.getWidth();
        int screenHeight = (int) screenSize.getHeight();
        
        System.out.println("Screen Width: " + screenWidth);
        System.out.println("Screen Height: " + screenHeight);
    }
}

In the above code, we used the getDefaultToolkit() method of the Toolkit class to obtain a Toolkit object. Then we used the getScreenSize() method to get the size of the screen, and finally got the width and height of the screen through the getWidth() and getHeight() methods.

bannerAds