Java GUI : Java window (JFrame) at center of the screen – I

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class  main  extends JFrame implements  ActionListener 
{

    JButton btn;
		
	  public main()
	  {
		  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		  setTitle("codeTpoint.com ");
		  setSize(500,400);
		  setLayout(new FlowLayout());
		  btn= new JButton("Click Here to position this frame  to center of the screen!");
		  
		  btn.addActionListener(this);
		  add(btn);
	      setVisible(true);
		  
	  }

	@Override
	public void actionPerformed(ActionEvent arg) {
	
		if(arg.getSource()==(JButton) btn)
		{
			// Get the size of the screen
	        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

	        // Determine the new location of the window
	        
	        int w = this.getSize().width;
	        int h = this.getSize().height;
	        
	        JOptionPane.showMessageDialog(null,"Window Size::"+w+ "|"+h ,"My Frame Size",JOptionPane.INFORMATION_MESSAGE);
	        
	
	        JOptionPane.showMessageDialog(null,"Screen Size::"+dim.width+ "|"+dim.height ,"Screen Size",JOptionPane.INFORMATION_MESSAGE);
	        
	        int x = (dim.width-w)/2;
	        int y = (dim.height-h)/2;

	        // Move the window
	        this.setLocation(x, y);
		}
	}			 

}

class  win1
{
	 
	  public  static void main(String codetpoint[])
	  {
	  
	     new main();
	   		  
	  }

	
	
}

Output

java Frame