Swing中设置屏幕居中的两种方法

/***
* 注意:window是BasicToolBarUI.DragWindow, Dialog, Frame, JWindow 几个的父类
*
*/	
public void setCenter(Window window){
//	方法1
    GraphicsConfiguration gc = window.getGraphicsConfiguration();
    Rectangle bounds = gc.getBounds();
    int x = (int) (( bounds.getWidth() - window.getWidth() ) * 0.5);
    int y = (int) (( bounds.getHeight() - window.getHeight() )*0.5);

// 方法2
//  int x = (int)((Toolkit.getDefaultToolkit().getScreenSize().getWidth() - window.getWidth())*0.5);
//  int y = (int)((Toolkit.getDefaultToolkit().getScreenSize().getHeight() - window.getHeight() - Toolkit.getDefaultToolkit().getScreenInsets(gc).bottom)*0.5);	
    window.setLocation(x, y);
}

你可能感兴趣的:(swing)