首页 > 代码库 > JFrame居中

JFrame居中

//居中设置
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
int x = (screenSize.width - frame.getWidth())/ 2;
int y= (screenSize.height - frame.getHeight())/ 2;
frame.setLocation(x , y);

 

JDK1.4后提供方法,建议使用

frame.setLocationRelativeTo(owner); 

如果是主窗体,则

 frame.setLocationRelativeTo(null);