/***
* 注意: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中设置屏幕居中的两种方法
最新推荐文章于 2021-06-04 16:42:26 发布