Swing中JInternalFrame窗口平铺、级联等实现

import java.awt.Dimension;
import java.beans.PropertyVetoException;
import java.util.ArrayList;

import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;

public class FrameLayerManager {

    private static ArrayList<JInternalFrame> getVisibleFrameList(JDesktopPane desktopPane) {

        JInternalFrame[] allframes = desktopPane.getAllFrames();

        ArrayList<JInternalFrame> list = new ArrayList<>();

        for (JInternalFrame eachFrame : allframes) {
            if (eachFrame.isVisible()) {
                list.add(eachFrame);
            }
        }

        return list;
    }

    public static void cascade(JDesktopPane desktopPane) {
        ArrayList<JInternalFrame> list = getVisibleFrameList(desktopPane);

        int count = list.size();
        if (count == 0) {
            return;
        }

        for (int i = 0; i < count; i++) {
            JInternalFrame f = list.get(count - i - 1);

            if (!f.isClosed() && f.isIcon()) {
                try {
                    f.setIcon(false);
                } catch (PropertyVetoException ignored) {
                }
            }

            Dimension size = desktopPane.getSize();

            desktopPane.getDesktopManager().resizeFrame(f, i * 30, i * 30,
                    size.width - count * 30, size.height - count * 30);
        }
    }

    public static void horizontal(JDesktopPane desktopPane) {
        ArrayList<JInternalFrame> list = getVisibleFrameList(desktopPane);

        int count = list.size();
        if (count == 0) {
            return;
        }

        Dimension size = desktopPane.getSize();

        for (int i = 0; i < count; i++) {
            JInternalFrame f = list.get(i);

            if (!f.isClosed() && f.isIcon()) {
                try {
                    f.setIcon(false);
                } catch (PropertyVetoException ignored) {
                }
            }

            desktopPane.getDesktopManager().resizeFrame(f, 0,
                    size.height * i / count, size.width, size.height / count);
        }
    }

    public static void vertical(JDesktopPane desktopPane) {
        ArrayList<JInternalFrame> list = getVisibleFrameList(desktopPane);

        int count = list.size();
        if (count == 0)
            if (count == 0) {
                return;
            }

        Dimension size = desktopPane.getSize();

        for (int i = 0; i < count; i++) {
            JInternalFrame f =  list.get(i);

            if (!f.isClosed() && f.isIcon()) {
                try {
                    f.setIcon(false);
                } catch (PropertyVetoException ignored) {
                }
            }

            desktopPane.getDesktopManager().resizeFrame(f,
                    size.width * i / count, 0, size.width / count, size.height);
        }
    }

    public static void arrangeAll(JDesktopPane desktopPane) {
        ArrayList<JInternalFrame> list = getVisibleFrameList(desktopPane);

        int count = list.size();
        if (count == 0) {
            return;
        }

        int sqrt = (int) Math.sqrt(count);
        int rows = sqrt;
        int cols = sqrt;
        if (rows * cols < count) {
            cols++;
            if (rows * cols < count) {
                rows++;
            }
        }

        Dimension size = desktopPane.getSize();

        int w = size.width / cols;
        int h = size.height / rows;
        int x = 0;
        int y = 0;

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols && ((i * cols) + j < count); j++) {
                JInternalFrame f = list.get((i * cols) + j);

                if (!f.isClosed() && f.isIcon()) {
                    try {
                        f.setIcon(false);
                    } catch (PropertyVetoException ignored) {
                    }
                }

                desktopPane.getDesktopManager().resizeFrame(f, x, y, w, h);
                x += w;
            }
            y += h;
            x = 0;
        }
    }
}

你可能感兴趣的:(Swing中JInternalFrame窗口平铺、级联等实现)