I usually do something like the following to center the JFrame. You can add an offset to the variable wdwLeftas shown in the list to move the frame to the center. (The challenge setPreferredSize()is redundant and only there to do this demo.)
package testapplication;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class MyJFrame extends JFrame {
MyJFrame() {
super("Test");
Dimension screenSize = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
setPreferredSize(new Dimension(200, 200));
Dimension windowSize = new Dimension(getPreferredSize());
int wdwLeft = 300 + screenSize.width / 2 - windowSize.width / 2;
int wdwTop = screenSize.height / 2 - windowSize.height / 2;
pack();
setLocation(wdwLeft, wdwTop);
}
public static void main(final String [] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
final MyJFrame jf = new MyJFrame();
jf.setVisible(true);
}
}
);
}
}
You can add additional logic to make sure that the offset window is still completely on the screen, as defined getMaximumWindowBounds().