Java JFrame Size setSizeBasedOnResolution(final JFrame frame)

Here you can find the source of setSizeBasedOnResolution(final JFrame frame)

Description

Set the size of the JFrame based on the resolution of the screen

License

Open Source License

Parameter

Parameter Description
frame a parameter

Declaration

public static void setSizeBasedOnResolution(final JFrame frame) 

Method Source Code


//package com.java2s;
import javax.swing.*;
import java.awt.*;

public class Main {
    /**/*from  www  .  ja  v  a  2s  . com*/
     * Set the size of the JComponent based on the resolution of the screen
     *
     * @param component
     */
    public static void setSizeBasedOnResolution(final JComponent component) {
        Dimension dimension = getDimensionBasedOnResolution();
        component.setSize(dimension.width, dimension.height);
    }

    /**
     * Set the size of the JFrame based on the resolution of the screen
     *
     * @param frame
     */
    public static void setSizeBasedOnResolution(final JFrame frame) {
        Dimension dimension = getDimensionBasedOnResolution();
        frame.setSize(dimension.width, dimension.height);
    }

    /**
     * @return a Dimension object containing the resolution of the first display iff it exists
     *         800 x 600 will be returned as a default
     */
    private static Dimension getDimensionBasedOnResolution() {
        int width = 800;
        int height = 600;
        GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] graphicsDevices = graphicsEnvironment.getScreenDevices();
        if (graphicsDevices.length >= 1) {
            DisplayMode displayMode = graphicsDevices[0].getDisplayMode();
            width = displayMode.getWidth() - 60;
            height = displayMode.getHeight() - 60;
        }
        return new Dimension(width, height);
    }
}

Related

  1. positionFrame(final JFrame frame, final Dimension viewSize, final int addX, final int addY)
  2. resizeApp(JFrame app)
  3. restrictWindowMinimumSize(final JInternalFrame frame, final Dimension minSize)
  4. setMinMaxSizeFrame(JFrame frame)
  5. setMinSizeFrame(JFrame frame)
  6. setSizeWithinScreen(JFrame frame, int preferredWidth, int preferredHeight)
  7. showSplashWindow(String message, Font messageFont, int duration, Dimension windowSize, Window frameOwner)