/** * centerlize the component,that is locate the component in the * center of the screen * @param c the component to be centerlized */ public static void center(Component c) { center(c, c.getSize()); } /** * centerlize the component,whose size is dimension me * @param c the component to centerlized * @param me dimension the component to be shown */ public static void center(Component c, Dimension me) { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); c.setLocation(screenSize.width / 2 - me.width / 2, screenSize.height / 2 - me.height / 2); }
/** * make the component be in center of the parent component, * if the component exceeds the borders of the screen,then * make it in the screen * @param parent the parent component * @param me the component to be centerlized */ public static void center(Component parent,Component me){ Point p = parent.getLocation(); Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); Dimension psize = parent.getSize(); Dimension msize = me.getSize(); Point m = new Point((int)(p.getX()+(psize.width-msize.width)/2), (int)(p.getY()+(psize.height-msize.height)/2)); if(m.x+msize.width > screen.width ){ m.x = screen.width-msize.width; }
前两个参数是位置
后两个是窗口大小
* centerlize the component,that is locate the component in the
* center of the screen
* @param c the component to be centerlized
*/
public static void center(Component c) {
center(c, c.getSize());
}
/**
* centerlize the component,whose size is dimension me
* @param c the component to centerlized
* @param me dimension the component to be shown
*/
public static void center(Component c, Dimension me) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
c.setLocation(screenSize.width / 2 - me.width / 2, screenSize.height
/ 2 - me.height / 2);
}
/**
* make the component be in center of the parent component,
* if the component exceeds the borders of the screen,then
* make it in the screen
* @param parent the parent component
* @param me the component to be centerlized
*/
public static void center(Component parent,Component me){
Point p = parent.getLocation();
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Dimension psize = parent.getSize();
Dimension msize = me.getSize();
Point m = new Point((int)(p.getX()+(psize.width-msize.width)/2),
(int)(p.getY()+(psize.height-msize.height)/2));
if(m.x+msize.width > screen.width ){
m.x = screen.width-msize.width;
}
else if(m.x<0){
m.x=0;
}
if(m.y+msize.height > screen.height){
m.y = screen.height - msize.height;
}
else if(m.y<0){
m.y = 0;
}
me.setLocation(m.x,m.y);
}