code: public static void centerWindow (Window window)
      {
          Rectangle bounds;
          try {
              bounds = GraphicsEnvironment.getLocalGraphicsEnvironment().
                  getDefaultScreenDevice().getDefaultConfiguration().getBounds();
          } catch (Throwable t) {
              Toolkit tk = window.getToolkit();
              Dimension ss = tk.getScreenSize();
              bounds = new Rectangle(ss);
          }
  
          int width = window.getWidth(), height = window.getHeight();
          window.setBounds(bounds.x + (bounds.width-width)/2, bounds.y + (bounds.height-height)/2,
                           width, height);
      }
docs: Center the given window within the screen boundaries.
  
  @param window the window to be centered.