Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

public class Main {
    private static int windowXCenter = -1;
    private static int windowYCenter = -1;

    /**
     * This method packs, sets the size, and sets the position of the window given
     *
     * @param window
     */
    public static void centerAndPack(Window window) {
        window.pack();
        int x = getWindowXCenter() - (window.getWidth() / 2);
        int y = getWindowYCenter() - (window.getHeight() / 2);
        window.setLocation(x, y);
        window.setMinimumSize(new Dimension(window.getWidth(), window.getHeight()));
    }

    /**
     * Don't use this method unless you want all the centerAndPack calls to set the center of the window somewhere other
     * than the center!
     *
     * @return the windowXCenter
     */
    public static int getWindowXCenter() {
        if (windowXCenter == -1) {
            windowXCenter = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2;
        }
        return windowXCenter;
    }

    /**
     * @return the windowYCenter
     */
    public static int getWindowYCenter() {
        if (windowYCenter == -1) {
            windowYCenter = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2;
        }
        return windowYCenter;
    }
}