Example usage for java.awt Window setPreferredSize

List of usage examples for java.awt Window setPreferredSize

Introduction

In this page you can find the example usage for java.awt Window setPreferredSize.

Prototype

public void setPreferredSize(Dimension preferredSize) 

Source Link

Document

Sets the preferred size of this component to a constant value.

Usage

From source file:Main.java

public static void center(Window window) {
    window.pack();/*from w w  w .java  2  s.  c  o  m*/
    Rectangle bounds = window.getGraphicsConfiguration().getBounds();
    int width = window.getWidth();
    int height = window.getHeight();
    int centerX = bounds.x + bounds.width / 2;
    int centerY = bounds.y + bounds.height / 2;
    window.setLocation(centerX - width / 2, centerY - height / 2);
    window.setPreferredSize(new Dimension(width, height));
}

From source file:Main.java

public static void center(Window window, double ratio) {
    Rectangle bounds = window.getGraphicsConfiguration().getBounds();
    int width = bounds.width / 2;
    int height = bounds.height / 2;
    int centerX = (int) (bounds.x + bounds.width * ratio);
    int centerY = (int) (bounds.y + bounds.height * ratio);
    window.setLocation(centerX - width / 2, centerY - height / 2);
    window.setPreferredSize(new Dimension(width, height));
    window.pack();/*from   w  w  w.  j av a 2s.  c  o  m*/
}

From source file:Main.java

public static void centerWindow(Window window, int width, int height) {
    // Get screen size
    final Toolkit tk = Toolkit.getDefaultToolkit();
    final Dimension screensize = tk.getScreenSize();

    // Set window minimum size
    window.setMinimumSize(new Dimension((int) Math.floor(screensize.getWidth() * 0.3),
            (int) Math.floor(screensize.getHeight() * 0.3)));

    // Set window size
    if (width == 0f)
        width = (int) Math.floor(screensize.getWidth() * 0.8);

    if (height == 0f)
        height = (int) Math.floor(screensize.getHeight() * 0.8);

    window.setPreferredSize(new Dimension(width, height));

    int left = (int) (screensize.getWidth() - width) / 2;
    int right = (int) (screensize.getHeight() - height) / 2;
    ;//from w w  w  .  java  2s .c o  m

    // Center window
    window.setLocation(left, right);
}