Here you can find the source of setLocationRelativeToAndSizeToWindow(Component parent, Component child, Dimension max)
Parameter | Description |
---|---|
parent | The visible parent. |
child | The child to display. |
max | The maximum size of the window. |
public static void setLocationRelativeToAndSizeToWindow(Component parent, Component child, Dimension max)
//package com.java2s; /*// www . j a va 2 s.c om * org.openmicroscopy.shoola.util.ui.UIUtilities * *------------------------------------------------------------------------------ * Copyright (C) 2006-2015 University of Dundee. All rights reserved. * * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * *------------------------------------------------------------------------------ */ import java.awt.Component; import java.awt.Dimension; import java.awt.Rectangle; import java.awt.Toolkit; public class Main { /** * Sets the location of the specified child relative to the location * of the specified parent and then makes it visible, and size to fill * the window. * This method is mainly useful for windows, frames and dialogs. * * @param parent The visible parent. * @param child The child to display. * @param max The maximum size of the window. */ public static void setLocationRelativeToAndSizeToWindow(Component parent, Component child, Dimension max) { setLocationRelativeToAndSizeToWindow(parent.getBounds(), child, max); } /** * Sets the location of the specified child relative to the location * of the specified parent and then makes it visible, and size to fill window. * This method is mainly useful for windows, frames and dialogs. * * @param parentBounds The bounds of the visible parent. * @param child The child to display. * @param max The maximum size of the window. */ public static void setLocationRelativeToAndSizeToWindow(Rectangle parentBounds, Component child, Dimension max) { if (child == null) return; if (parentBounds == null) parentBounds = new Rectangle(0, 0, 5, 5); if (max == null) max = new Dimension(5, 5); int x = (int) (parentBounds.getX() + parentBounds.getWidth()); int y = (int) parentBounds.getY(); int childWidth = child.getWidth(); int childHeight = child.getHeight(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); if (x + childWidth > screenSize.getWidth()) { if (childWidth < parentBounds.getX()) x = (int) (parentBounds.getX()) - childWidth; else x = (int) (screenSize.getWidth() - childWidth); } child.setLocation(x, y); int newHeight = (int) screenSize.getHeight() - y - 10; int newWidth = (int) screenSize.getWidth() - x - 10; if (newWidth > childWidth) childWidth = newWidth; if (newHeight > childHeight) childHeight = newHeight; if (childWidth > max.getWidth()) childWidth = (int) max.getWidth(); if (childHeight > max.getHeight()) childHeight = (int) max.getHeight(); child.setSize(childWidth, childHeight); child.setVisible(true); } }