Set JFrame Center Position Java Source Code

This is simple, just make a method like below and pass your JFrame object to the method parameter.

public static void setCenterPos(JFrame frame){
    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (int) ((screen.getWidth() - frame.getWidth()) / 2);
    int y = (int) ((screen.getHeight() - frame.getHeight()) / 2);
    frame.setLocation(x, y);

Leave a Reply

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

Post Navigation