⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 borderlayout.java

📁 j2me设计的界面包
💻 JAVA
字号:
/*
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */
package com.sun.lwuit.layouts;

import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.geom.*;

/**
 * A border layout lays out a container, arranging and resizing its 
 * components to fit in five regions: north, south, east, west, and center. 
 * Each region may contain no more than one component, and is identified by a 
 * corresponding constant: NORTH, SOUTH, EAST, WEST, and CENTER. 
 * When adding a component to a container with a border layout, use one of 
 * these five constants.
 *
 * @author Nir
 */
public class BorderLayout extends Layout {

    private Component north;
    private Component south;
    private Component center;
    private Component west;
    private Component east;
    /**
     * The north layout constraint (top of container).
     */
    public static final String NORTH = "North";
    /**
     * The south layout constraint (bottom of container).
     */
    public static final String SOUTH = "South";
    /**
     * The center layout constraint (middle of container)
     */
    public static final String CENTER = "Center";
    /**
     * The west layout constraint (left of container).
     */
    public static final String WEST = "West";
    /**
     * The east layout constraint (right of container).
     */
    public static final String EAST = "East";

    /** 
     * Creates a new instance of BorderLayout 
     */
    public BorderLayout() {
    }

    /**
     * @inheritDoc
     */
    public void addLayoutComponent(Object name, Component comp, Container c) {
        Component previous = null;

        /* Assign the component to one of the known regions of the layout.
         */
        if (CENTER.equals(name)) {
            previous = center;
            center = comp;
        } else if (NORTH.equals(name)) {
            previous = north;
            north = comp;
        } else if (SOUTH.equals(name)) {
            previous = south;
            south = comp;
        } else if (EAST.equals(name)) {
            previous = east;
            east = comp;
        } else if (WEST.equals(name)) {
            previous = west;
            west = comp;
        } else {
            throw new IllegalArgumentException("cannot add to layout: unknown constraint: " + name);
        }
        if (previous != null && previous != comp) {
            c.removeComponent(previous);
        }
    }

    /**
     * @inheritDoc
     */
    public void removeLayoutComponent(Component comp) {
        if (comp == center) {
            center = null;
        } else if (comp == north) {
            north = null;
        } else if (comp == south) {
            south = null;
        } else if (comp == east) {
            east = null;
        } else if (comp == west) {
            west = null;
        }
    }

    public Object getComponentConstraint(Component comp) {
        if (comp == center) {
            return CENTER;
        } else if (comp == north) {
            return NORTH;
        } else if (comp == south) {
            return SOUTH;
        } else if (comp == east) {
            return EAST;
        } else {
            return WEST;
        }
    }

    /**
     * @inheritDoc
     */
    public void layoutContainer(Container target) {
        int top = target.getStyle().getPadding(Component.TOP);
        int bottom = target.getLayoutHeight() - target.getBottomGap() - target.getStyle().getPadding(Component.BOTTOM);
        int left = target.getStyle().getPadding(Component.LEFT);
        int right = target.getLayoutWidth() - target.getSideGap() - target.getStyle().getPadding(Component.RIGHT);
        int targetWidth = target.getWidth();
        int targetHeight = target.getHeight();

        Component c = null;

        if (north != null) {
            c = north;
            c.setWidth(right - left - c.getStyle().getMargin(Component.LEFT) - c.getStyle().getMargin(Component.RIGHT));
            c.setHeight(Math.min(targetHeight, c.getPreferredH())); //verify I want to use tge prefered size
            c.setX(left + c.getStyle().getMargin(Component.LEFT));
            c.setY(top + c.getStyle().getMargin(Component.TOP));
            top += (c.getHeight() + c.getStyle().getMargin(Component.TOP) + c.getStyle().getMargin(Component.BOTTOM));

        }
        if (south != null) {
            c = south;
            c.setWidth(right - left - c.getStyle().getMargin(Component.LEFT) - c.getStyle().getMargin(Component.RIGHT));
            c.setHeight(Math.min(targetHeight, c.getPreferredH())); //verify I want to use tge prefered size
            c.setX(left + c.getStyle().getMargin(Component.LEFT));
            c.setY(bottom - c.getHeight() - c.getStyle().getMargin(Component.TOP));

            bottom -= (c.getHeight() + c.getStyle().getMargin(Component.TOP) + c.getStyle().getMargin(Component.BOTTOM));

        }
        if (east != null) {
            c = east;
            c.setWidth(Math.min(targetWidth, c.getPreferredW()));
            c.setHeight(bottom - top - c.getStyle().getMargin(Component.TOP) - c.getStyle().getMargin(Component.BOTTOM)); //verify I want to use tge prefered size
            c.setX(right - c.getWidth() - c.getStyle().getMargin(Component.LEFT));
            c.setY(top + c.getStyle().getMargin(Component.TOP));

            right -= (c.getWidth() + c.getStyle().getMargin(Component.LEFT) + c.getStyle().getMargin(Component.RIGHT));

        }
        if (west != null) {
            c = west;
            c.setWidth(Math.min(targetWidth, c.getPreferredW()));
            c.setHeight(bottom - top - c.getStyle().getMargin(Component.TOP) - c.getStyle().getMargin(Component.BOTTOM)); //verify I want to use tge prefered size
            c.setX(left + c.getStyle().getMargin(Component.LEFT));
            c.setY(top + c.getStyle().getMargin(Component.TOP));

            left += (c.getWidth() + c.getStyle().getMargin(Component.LEFT) + c.getStyle().getMargin(Component.RIGHT));

        }

        if (center != null) {
            c = center;
            c.setWidth(right - left - c.getStyle().getMargin(Component.LEFT) - c.getStyle().getMargin(Component.RIGHT));
            c.setHeight(bottom - top - c.getStyle().getMargin(Component.TOP) - c.getStyle().getMargin(Component.BOTTOM)); //verify I want to use the remaining size
            c.setX(left + c.getStyle().getMargin(Component.LEFT));
            c.setY(top + c.getStyle().getMargin(Component.TOP));
//            if(c instanceof Container) {
//                ((Container)c).layoutContainer();
//            }
        }
    }

    /**
     * @inheritDoc
     */
    public Dimension getPreferredSize(Container parent) {
        Dimension dim = new Dimension(0, 0);

        if (east != null) {
            dim.setWidth(east.getPreferredW() + east.getStyle().getMargin(Component.LEFT) + east.getStyle().getMargin(Component.RIGHT));
            dim.setHeight(Math.max(east.getPreferredH() + east.getStyle().getMargin(Component.TOP) + east.getStyle().getMargin(Component.BOTTOM), dim.getHeight()));
        }
        if (west != null) {
            dim.setWidth(dim.getWidth() + west.getPreferredW() + west.getStyle().getMargin(Component.LEFT) + west.getStyle().getMargin(Component.RIGHT));
            dim.setHeight(Math.max(west.getPreferredH() + west.getStyle().getMargin(Component.TOP) + west.getStyle().getMargin(Component.BOTTOM), dim.getHeight()));
        }
        if (center != null) {
            dim.setWidth(dim.getWidth() + center.getPreferredW() + center.getStyle().getMargin(Component.LEFT) + center.getStyle().getMargin(Component.RIGHT));
            dim.setHeight(Math.max(center.getPreferredH() + center.getStyle().getMargin(Component.TOP) + center.getStyle().getMargin(Component.BOTTOM), dim.getHeight()));
        }
        if (north != null) {
            dim.setWidth(Math.max(north.getPreferredW() + north.getStyle().getMargin(Component.LEFT) + north.getStyle().getMargin(Component.RIGHT), dim.getWidth()));
            dim.setHeight(dim.getHeight() + north.getPreferredH() + north.getStyle().getMargin(Component.TOP) + north.getStyle().getMargin(Component.BOTTOM));
        }

        if (south != null) {
            dim.setWidth(Math.max(south.getPreferredW() + south.getStyle().getMargin(Component.LEFT) + south.getStyle().getMargin(Component.RIGHT), dim.getWidth()));
            dim.setHeight(dim.getHeight() + south.getPreferredH() + south.getStyle().getMargin(Component.TOP) + south.getStyle().getMargin(Component.BOTTOM));
        }

        dim.setWidth(dim.getWidth() + parent.getStyle().getPadding(Component.LEFT) + parent.getStyle().getPadding(Component.RIGHT));
        dim.setHeight(dim.getHeight() + parent.getStyle().getPadding(Component.TOP) + parent.getStyle().getPadding(Component.BOTTOM));
        return dim;
    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -