Easiest way to make JFrame center

Complete source code below will show you, the easiest way to make JFrame get to the center of screen by only one line of code. We will use method setLocationRelativeTo(null) to make our JFrame get to the center. Remember, when you want to use this code, you must put it after you set JFrame size.

*****************************************************************
COMPLETE SOURCE CODE FOR : EasiestWayToMakeJFrameCenter.java
*****************************************************************


import javax.swing.JFrame;

public class EasiestWayToMakeJFrameCenter
{
public static void main(String[]args)
{
JFrame frame=new JFrame("Easiest way to make JFrame center");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,400);

//setLocationRelativeTo(null) will make JFrame get to the center
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}


*****************************************************************
JUST COMPILE AND EXECUTE IT
*****************************************************************

RELAXING NATURE VIDEO