Here you can find the source of showCentered(JPanel panel)
public static void showCentered(JPanel panel)
//package com.java2s; //License from project: Apache License import javax.swing.*; import java.awt.*; public class Main { public static void showCentered(JPanel panel) { JFrame frame = new JFrame(); frame.setContentPane(panel);/*w w w. ja va 2s. c o m*/ frame.pack(); center(frame); frame.setVisible(true); } /** * Shows the window centered on its parent window, or the screen if there is no parent window. */ public static void showCentered(Window window) { center(window); window.setVisible(true); } /** * Centers the window on its parent window, or the screen if there is no parent window. */ public static void center(Window window) { Point origin; Dimension parentSize; Dimension maxSize = getMaxSize(window); Container parent = window.getParent(); if ((parent != null) && parent.isShowing()) { origin = parent.getLocationOnScreen(); parentSize = parent.getSize(); } else { Insets screenInsets = getScreenInsets(window); origin = new Point(screenInsets.left, screenInsets.top); parentSize = maxSize; } Dimension windowSize = new Dimension(window.getSize()); if (windowSize.width > maxSize.width) { windowSize.width = maxSize.width; } if (windowSize.height > maxSize.height) { windowSize.height = maxSize.height; } window.setBounds(origin.x + parentSize.width / 2 - windowSize.width / 2, origin.y + parentSize.height / 2 - windowSize.height / 2, windowSize.width, windowSize.height); } public static Dimension getMaxSize(Window frame) { Toolkit toolkit = Toolkit.getDefaultToolkit(); GraphicsConfiguration config = frame.getGraphicsConfiguration(); Dimension availableScreenSize = new Dimension(toolkit.getScreenSize()); Insets insets = toolkit.getScreenInsets(config); availableScreenSize.width -= (insets.left + insets.right); availableScreenSize.height -= (insets.top + insets.bottom); return availableScreenSize; } private static Insets getScreenInsets(Window window) { Toolkit toolkit = Toolkit.getDefaultToolkit(); GraphicsConfiguration config = window.getGraphicsConfiguration(); return toolkit.getScreenInsets(config); } }