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

📄 boxlayout.java.svn-base

📁 j2me设计的界面包
💻 SVN-BASE
字号:
/*
 * 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.*;

/**
 * Layout manager that places elements in a row or column according to 
 * box orientation
 *
 * @author Chen Fishbein
 */
public class BoxLayout extends Layout{
    
    /**
     * Horizontal layout where components are arranged from left to right
     */
    public static final int X_AXIS = 1;

    /**
     * Vertical layout where components are arranged from top to bottom
     */
    public static final int Y_AXIS = 2;
    
    private int axis;
    
    /** 
     * Creates a new instance of BoxLayout
     * 
     * @param axis the axis to lay out components along. 
     * Can be: BoxLayout.X_AXIS or BoxLayout.Y_AXIS
     */
    public BoxLayout(int axis) {
        this.axis = axis;
    }
    
    /**
     * @inheritDoc
     */
    public void layoutContainer(Container parent) {        
        int width = parent.getLayoutWidth() - parent.getSideGap() - parent.getStyle().getPadding(Component.RIGHT);
        int height = parent.getLayoutHeight() - parent.getBottomGap() - parent.getStyle().getPadding(Component.BOTTOM);
        int x = parent.getStyle().getPadding(Component.LEFT);
        int y = parent.getStyle().getPadding(Component.TOP);
        int numOfcomponents = parent.getComponentCount();
        
        for(int i=0; i< numOfcomponents; i++){
            Component cmp = parent.getComponentAt(i);
            
            if(axis == Y_AXIS){
                int cmpBottom = height;
                int cmpH = cmp.getPreferredH();
                
                y += cmp.getStyle().getMargin(Component.TOP);
                
                if(y >= cmpBottom){
                    cmpH = 0;
                }else if(y + cmpH > cmpBottom){
                    cmpH = cmpBottom - y - cmp.getStyle().getMargin(Component.BOTTOM);
                }
                
                cmp.setWidth(width - cmp.getStyle().getMargin(Component.LEFT) - cmp.getStyle().getMargin(Component.RIGHT));
                cmp.setHeight(cmpH);
                cmp.setX(x + cmp.getStyle().getMargin(Component.LEFT));
                cmp.setY(y);
                y += cmp.getHeight() + cmp.getStyle().getMargin(Component.BOTTOM);
            }else{
                int cmpRight = width;
                int cmpW = cmp.getPreferredW();
                
                x += cmp.getStyle().getMargin(Component.RIGHT);

                if(x >= cmpRight){
                    cmpW = 0;
                }else if(x + cmpW > cmpRight){
                    cmpW = cmpRight - x - cmp.getStyle().getMargin(Component.RIGHT);
                }
                
                cmp.setWidth(cmpW);
                cmp.setHeight(height- cmp.getStyle().getMargin(Component.TOP) - cmp.getStyle().getMargin(Component.BOTTOM));
                cmp.setX(x);
                cmp.setY(y + cmp.getStyle().getMargin(Component.TOP));
                x += cmp.getWidth() + cmp.getStyle().getMargin(Component.RIGHT);
            }
        }
    }
    
    /**
     * @inheritDoc
     */
    public Dimension getPreferredSize(Container parent) {
        int width = 0;
        int height = 0;

        int numOfcomponents = parent.getComponentCount();
        for(int i=0; i< numOfcomponents; i++){
            Component cmp = parent.getComponentAt(i);
            
            if(axis == Y_AXIS){
                height += cmp.getPreferredH()+ cmp.getStyle().getMargin(Component.TOP) + cmp.getStyle().getMargin(Component.BOTTOM);
                width = Math.max(width, cmp.getPreferredW());
            }else{
                height = Math.max(height, cmp.getPreferredH());
                width += cmp.getPreferredW()+ cmp.getStyle().getMargin(Component.LEFT) + cmp.getStyle().getMargin(Component.RIGHT);
            }
        }
        return new Dimension(width + parent.getStyle().getPadding(Component.LEFT)+ parent.getStyle().getPadding(Component.RIGHT), 
        height + parent.getStyle().getPadding(Component.TOP)+ parent.getStyle().getPadding(Component.BOTTOM));
    }  
    
}

⌨️ 快捷键说明

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