Toolkit kit = Toolkit.getDefaultToolkit(); // 定义工具包
Dimension screenSize = kit.getScreenSize(); // 获取屏幕的尺寸
int screenWidth = screenSize.width/2; // 获取屏幕的宽
int screenHeight = screenSize.height/2; // 获取屏幕的高
int height = this.getHeight();
int width = this.getWidth();
setLocation(screenWidth-width/2, screenHeight-height/2);
当然,如果编译不过的话请确认你有无引用以下三个包
import javax.swing.JFrame;
import java.awt.Toolkit;
import java.awt.Dimension;