center Component - Java java.awt

Java examples for java.awt:Component

Description

center Component

Demo Code

// 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;

public class Main {
    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);
    }//from   w  ww  .  ja  va2s .co  m
}

Related Tutorials