How to set JFrame to appear centered, regardless of monitor resolution?

This method has a special effect when you pass it a null. According to the Javadoc:
If the component is null, or the GraphicsConfiguration associated with this component is null, the window is placed in the center of the screen.
This should be done after setting the size or calling pack(), but before setting it visible, like this:
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);