获取组件居中的位置坐标

public static int[] getCenterLocation(Component componet){
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int width = screenSize.width;
		int height = screenSize.height;
		int left = (width - componet.getSize().width) / 2;
		int top = (height - componet.getSize().height) /2;
		int location[] = {left,top};
		return location;
	}

调用:

ExportFrame export = ExportFrame.getInstance();
            	int location[] = LocationUtil.getCenterLocation(export);
            	export.setLocation(location[0], location[1]);
            	export.setVisible(true);

猜你喜欢

转载自huangqiqing123.iteye.com/blog/1390090
今日推荐