Java examples for java.awt:Component
align Components
// Copyright (c) 1996 - 1999, 2003 by Yoshiki Shibata. All rights reserved. //package com.java2s; import java.awt.Component; import java.awt.Dimension; import java.awt.Point; import java.awt.Toolkit; public class Main { private static Dimension SCREEN_SIZE = Toolkit.getDefaultToolkit() .getScreenSize();/* w w w . j av a2 s .c o m*/ static public void alignComponents(Component child, Component parent) { Point location = parent.getLocation(); Dimension parentSize = parent.getSize(); Dimension childSize = child.getSize(); if ((location.x - childSize.width) > 0) { location.x -= childSize.width; child.setLocation(location); } else if ((location.x + parentSize.width + childSize.width) < SCREEN_SIZE.width) { location.x += parentSize.width; child.setLocation(location); } else centerComponent(child, location, parentSize); } static public void centerComponent(Component child, Point parentLocation, Dimension parentSize) { Dimension childSize = child.getSize(); if (childSize.width > parentSize.width) childSize.width = parentSize.width; if (childSize.height > parentSize.height) childSize.height = parentSize.height; child.setLocation(parentLocation.x + parentSize.width / 2 - childSize.width / 2, parentLocation.y + parentSize.height / 2 - childSize.height / 2); } }