取出Screen的大小,和本身界面的大小,
算出界面的左上角坐标即可
示例如下:
importjavax.swing.*;
importjava.awt.*;
publicclassFrameCentreTestextendsJFrame{
publicstaticvoidmain(String[]args){
JFrameframe=newJFrame("centre");
Dimensionscreensize=Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(400,400);
Dimensionframesize=frame.getSize();
intx=(int)screensize.getWidth()/2-(int)framesize.getWidth()/2;
inty=(int)screensize.getHeight()/2-(int)framesize.getHeight()/2;
frame.setLocation(x,y);
frame.setVisible(true);
}
}