Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import java.awt.Dimension;
import java.awt.Point;

import java.awt.Window;
import javax.swing.SwingConstants;

public class Main {
    public static Window showBelow(Component parent, Window window) {
        return showBelow(parent, window, SwingConstants.CENTER);
    }

    public static Window showBelow(Component parent, Window window, int horizontalAlignment) {
        final int DISTANCE = 2;

        if (null == parent)
            throw new IllegalArgumentException("parent null");
        if (null == window)
            throw new IllegalArgumentException("parent null");
        if (!((SwingConstants.LEADING == horizontalAlignment) || (SwingConstants.TRAILING == horizontalAlignment)
                || (SwingConstants.CENTER == horizontalAlignment)
                || (SwingConstants.SOUTH == horizontalAlignment))) {

            throw new IllegalArgumentException("Illegal horizontal alignment " + horizontalAlignment
                    + " should be either SwingConstants.LEADING or "
                    + "SwingConstants.TRAILING or SwingConstants.CENTER");
        }

        window.pack();

        Dimension windowSize = window.getPreferredSize();
        Dimension parentSize = parent.getSize();

        Point loc = parent.getLocationOnScreen();

        int newX;

        if ((SwingConstants.CENTER == horizontalAlignment) || (SwingConstants.SOUTH == horizontalAlignment)) {

            newX = (parentSize.width - windowSize.width) / 2 + loc.x;
        } else if (SwingConstants.TRAILING == horizontalAlignment) {
            newX = loc.x + parentSize.width - windowSize.width;
        } else {
            newX = loc.x;
        }

        window.setLocation(newX, (loc.y + parentSize.height + DISTANCE));

        window.setVisible(true);

        return window;
    }
}