📄 gridlayout.java.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.Dimension;
/**
* Components are arranged in an equaly sized grid based on available space
*
* @author Chen Fishbein
*/
public class GridLayout extends Layout{
private int rows;
private int columns;
/**
* Creates a new instance of GridLayout with the given rows and columns
*
* @param rows - the rows, with the value zero meaning any number of rows.
* @param columns - the columns, with the value zero meaning any number of
* columns.
*/
public GridLayout(int rows, int columns) {
this.rows = rows;
this.columns = columns;
}
/**
* @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();
int cmpWidth = (width)/columns;
int cmpHeight;
if (numOfcomponents > rows * columns) {
cmpHeight = (height)/(numOfcomponents/columns +1);//actual rows number
} else {
cmpHeight = (height)/rows;
}
int row = 0;
for(int i=0; i< numOfcomponents; i++){
Component cmp = parent.getComponentAt(i);
cmp.setWidth(cmpWidth - cmp.getStyle().getMargin(Component.LEFT)- cmp.getStyle().getMargin(Component.RIGHT));
cmp.setHeight(cmpHeight- cmp.getStyle().getMargin(Component.TOP)- cmp.getStyle().getMargin(Component.BOTTOM));
cmp.setX(x + (i%columns)*cmpWidth + cmp.getStyle().getMargin(Component.LEFT));
cmp.setY(y + row*cmpHeight + cmp.getStyle().getMargin(Component.TOP));
if((i + 1)%columns == 0){
row++;
}
}
}
/**
* @inheritDoc
*/
public Dimension getPreferredSize(Container parent) {
int width = 0;
int height = 0;
int numOfcomponents = parent.getComponentCount();
int row = 0;
for(int i=0; i< numOfcomponents; i++){
Component cmp = parent.getComponentAt(i);
width = Math.max(width, cmp.getPreferredW() + cmp.getStyle().getMargin(Component.LEFT)+ cmp.getStyle().getMargin(Component.RIGHT));
height = Math.max(height, cmp.getPreferredH()+ cmp.getStyle().getMargin(Component.TOP)+ cmp.getStyle().getMargin(Component.BOTTOM));
}
if(columns > 1){
width = width*columns;
}
if(rows > 1){
if(numOfcomponents>rows*columns){ //if there are more components than planned
height = height * (numOfcomponents/columns +1);
}else{
height = height*rows;
}
}
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 + -