Here you can find the source of centerFrame(JFrame frame)
public static void centerFrame(JFrame frame)
//package com.java2s; // GNU General Public License: // import java.awt.*; import javax.swing.*; public class Main { private static final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); public static void centerFrame(JFrame frame) { Dimension frameSize = frame.getSize(); frame.setLocation(what(frameSize.width, screenSize.width), what(frameSize.height, screenSize.height)); frame.setVisible(true);/*from www . j ava 2s .c om*/ } private static int what(int forgndLen, int bkgndLen, int offset) { int x = offset + (bkgndLen - forgndLen) / 2; return (x > 0 ? x : 0); } private static int what(int forgndLen, int bkgndLen) { return what(forgndLen, bkgndLen, 0); } }