Here you can find the source of maximizeWindowWithMargin(final Window window, final int margin, final Dimension maxSize)
Parameter | Description |
---|---|
window | window to be resized |
margin | margin to leave around the window |
maxSize | optional parameter defining a maximum size |
public static void maximizeWindowWithMargin(final Window window, final int margin, final Dimension maxSize)
//package com.java2s; //License from project: Apache License import java.awt.Dimension; import java.awt.GraphicsConfiguration; import java.awt.GraphicsEnvironment; import java.awt.Insets; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.Window; public class Main { /**//from w w w . ja v a2 s. c o m * Resizes a window by setting its bounds to maximum that fits inside the default screen having the specified margin around it, and centers the window on * the screen. * * <p> * The implementation takes the screen insets (for example space occupied by task bar) into account. * </p> * * @param window window to be resized * @param margin margin to leave around the window * @param maxSize optional parameter defining a maximum size */ public static void maximizeWindowWithMargin(final Window window, final int margin, final Dimension maxSize) { // Maybe use window.getGraphicsConfiguration() (it probably accounts multi-screens!) // Edit: it does, but setLocationRelativeTo() at the end will always use the default screen device! GraphicsConfiguration gconfig = window.getGraphicsConfiguration(); if (gconfig == null) GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice().getDefaultConfiguration(); final Rectangle bounds = gconfig.getBounds(); final Insets insets = Toolkit.getDefaultToolkit().getScreenInsets( gconfig); final int width = bounds.width - insets.left - insets.right - margin * 2; final int height = bounds.height - insets.top - insets.bottom - margin * 2; if (maxSize == null) window.setSize(width, height); else window.setSize(Math.min(width, maxSize.width), Math.min(height, maxSize.height)); // And finally center on screen // window.setLocationRelativeTo( null ) always centers on the main screen, so: window.setLocationRelativeTo(window.getOwner()); } }