📄 gridbaglayout.java
字号:
package java.awt;import java.util.Hashtable;/** * class GridBagLayout - * * Copyright (c) 1998 * Transvirtual Technologies, Inc. All rights reserved. * * See the file "license.terms" for information on usage and redistribution * of this file. * * @author J.Mehlitz */public class GridBagLayout implements LayoutManager2, java.io.Serializable{ final protected static int MINSIZE = 1; final protected static int PREFERREDSIZE = 2; final protected static int ACTUALSIZE = 3; final protected static int MAXGRIDSIZE = 100;/* NB: Sun doesn't hardcode */ final private static long serialVersionUID = 8838754796412211005L;/** @serial */ protected Hashtable comptable = new Hashtable();/** @serial */ protected GridBagConstraints defaultConstraints = new GridBagConstraints();// XXX fix serial form! /** @serial *///protected GridBagLayoutInfo layoutInfo; /** @serial */ public int[] columnWidths;/** @serial */ public int[] rowHeights;/** @serial */ public double[] columnWeights;/** @serial */ public double[] rowWeights; transient Point offset = new Point(); private static Component[][] defGrid = new Component[MAXGRIDSIZE][MAXGRIDSIZE];protected void AdjustForGravity(GridBagConstraints cons, Rectangle display) { throw new kaffe.util.NotImplemented(GridBagLayout.class.getName() + ".AdjustForGravity()");}protected void ArrangeGrid(Container container) { layoutContainer(container);}public GridBagLayout() {}public void addLayoutComponent( Component c, Object constraints) { try { if (constraints != null) { setConstraints(c, (GridBagConstraints)constraints); } } catch (ClassCastException _) { throw new IllegalArgumentException("not GridBagConstraints"); }}public void addLayoutComponent( String name, Component c) {}void adjustWeightsRelative( Container parent) { Insets pin = parent.getInsets(); int i; int nxw = 0; int nyw = 0; int sw = sumWidths(); int sh = sumHeights(); double dd; double twx = 0; double twy = 0; for ( i=0; i<columnWeights.length; i++) { dd = columnWeights[i]; if ( dd > 0.0 ) { twx += dd; nxw++; } } for ( i=0; i<rowWeights.length; i++) { dd = rowWeights[i]; if ( dd > 0.0 ) { twy += dd; nyw++; } } int piw = pin.left + pin.right; int pih = pin.top + pin.bottom; if ( nxw > 0 ) { int dx = parent.width - sw - piw; for ( i=0; i<columnWeights.length-1; i++){ double cw = columnWeights[i]; if ( cw > 0.0 ) { int dcx = (int)(cw/twx*dx); columnWidths[i] += dcx; twx -= cw; dx -= dcx; } if ( twx == 0) break; } if ( dx > 0) columnWidths[i] += dx; offset.x = 0; } else { offset.x = (parent.width - sw - piw) / 2; } if ( nyw > 0) { int dy = parent.height - sh - pih; for ( i=0; i<rowWeights.length-1; i++){ double rw = rowWeights[i]; if ( rw > 0.0 ) { int dcy = (int)(rw/twy*dy); rowHeights[i] += dcy; twy -= rw; dy -= dcy; } if ( twy == 0) break; } if ( dy > 0) rowHeights[i] += dy; offset.y = 0; } else { offset.y = (parent.height - sh - pih) / 2; }}void clearDefGrid() { for ( int y=0; y<MAXGRIDSIZE; y++) { for ( int x=0; x<MAXGRIDSIZE; x++) { defGrid[x][y] = null; } }}Component componentAt( Container parent, int x, int y) { int nc = parent.getComponentCount(); for ( int i=0; i<nc; i++ ) { Component c = parent.getComponent( i); GridBagConstraints gbc = lookupConstraints( c); if ( gbc.gridx <= x && gbc.gridy <= y && gbc.gridx + gbc.gridwidth > x && gbc.gridy + gbc.gridheight > y ) { return c; } } return null;}void dump() { if ( (columnWidths == null) || (rowHeights == null ) ){ return; } int i; System.out.print( "\t\t"); for ( i=0; i<columnWidths.length; i++) { System.out.print( columnWidths[i] + "[" + columnWeights[i] + "]" ); System.out.print( '\t'); } System.out.println(); for ( i=0; i<rowHeights.length; i++) { System.out.print( rowHeights[i] + "[" + rowWeights[i] + "]" ); for ( int i2=0; i2<columnWidths.length; i2++ ) { Component c = defGrid[i2][i]; System.out.print( '\t'); System.out.print( (c != null) ? Integer.toHexString( c.hashCode() ) : "-------" ); System.out.print( '\t'); } System.out.println(); } System.out.println();}void fillGrid( Container parent, int mxr, int myr, int mode ) { int maxX = 0; int maxY = 0; int x, y, di; GridBagConstraints cc; Dimension cd = new Dimension(); // get max cell extends for ( y=MAXGRIDSIZE-1; y>-1; y--) { if ( defGrid[MAXGRIDSIZE-1][y] != null ) { maxX = Math.max( maxX, mxr); } else { for ( x=MAXGRIDSIZE-1; x>-1; x--) { if ( defGrid[x][y] != null ) { maxX = Math.max( maxX, x+1); break; } } } } for ( x=MAXGRIDSIZE-1; x>-1; x--) { if ( defGrid[x][MAXGRIDSIZE-1] != null ) { maxY = Math.max( maxY, myr); } else { for ( y=MAXGRIDSIZE-1; y>-1; y--) { if ( defGrid[x][y] != null ) { maxY = Math.max( maxY, y+1); break; } } } } boolean allocNew = (columnWidths == null) || (rowHeights == null) || (columnWidths.length != maxX) || (rowHeights.length != maxY); if ( allocNew ) { columnWidths = new int[maxX]; columnWeights = new double[maxX]; rowHeights = new int[maxY]; rowWeights = new double[maxY]; } else { for ( x=0; x<maxX; x++ ) { columnWidths[x] = 0; columnWeights[x] = 0.0; } for ( y=0; y<maxY; y++ ) { rowHeights[y] = 0; rowWeights[y] = 0.0; } } // convert relatives and remainders to real values updateConstraints( parent); //fill widths and heights Rectangle cRect = new Rectangle(); int nc = parent.getComponentCount(); for ( int idx=0; idx<nc; idx++) { Component c = parent.getComponent( idx); cc = lookupConstraints( c); cRect.setBounds( cc.gridx, cc.gridy, cc.gridwidth, cc.gridheight); if ( (cRect.width == 1) || (cRect.height == 1) ) { getComponentExt( c, cc, mode, cd); if ( cRect.width == 1) { columnWidths[cRect.x] = Math.max( columnWidths[cRect.x], cd.width ); } if ( cRect.height == 1) { rowHeights[cRect.y] = Math.max( rowHeights[cRect.y], cd.height); } } di = cRect.x + cRect.width - 1; columnWeights[di] = Math.max( columnWeights[di] ,cc.weightx); di = cRect.y + cRect.height - 1; rowWeights[di] = Math.max( rowWeights[di] ,cc.weighty); } /* first call of getComponentExt() / getPreferredSize() could * scramble the defGrid array ( cascaded GridBagLayouts ). * Use the (updated) Constraints!!! */ for ( x=0; x<maxX; x++) { if ( columnWidths[x] == 0 ) { for ( y=0; y<maxY; y++ ) { Component c = componentAt( parent, x, y);// Component c = defGrid[x][y]; if ( c != null ) { cc = lookupConstraints( c); getComponentExt( c, cc, mode, cd); cRect.setBounds( cc.gridx, cc.gridy, cc.gridwidth, cc.gridheight); columnWidths[x] = Math.max( columnWidths[x], cd.width / cRect.width ); } } } } for ( y=0; y<maxY; y++) { if ( rowHeights[y] == 0 ) { for ( x=0; x<maxX; x++ ) { Component c = componentAt(parent, x, y);// Component c = defGrid[x][y]; if ( c != null ) { cc = lookupConstraints( c); getComponentExt( c, cc, mode, cd); cRect.setBounds( cc.gridx, cc.gridy, cc.gridwidth, cc.gridheight); rowHeights[y] = Math.max( rowHeights[y], cd.height / cRect.height ); } } } } if ( mode == ACTUALSIZE) { adjustWeightsRelative( parent); }}Dimension getComponentExt( Component c, GridBagConstraints cc, int mode, Dimension dim) { dim.setSize( (mode == MINSIZE) ? c.getMinimumSize() : c.getPreferredSize()); dim.width += cc.insets.left + cc.insets.right + 2 * cc.ipadx; dim.height += cc.insets.top + cc.insets.bottom + 2 * cc.ipady; return dim;}public GridBagConstraints getConstraints( Component c) { return ((GridBagConstraints)lookupConstraints(c).clone());}void getGrid( Container parent, int mode ) { int x1 = 0; int y1 = 0; int cx, cy; int mxr = -1; int myr = -1; GridBagConstraints lcc = null; int nc = parent.getComponentCount(); synchronized( defGrid) { clearDefGrid(); for ( int i=0; i<nc; i++){ Component c = parent.getComponent(i); GridBagConstraints cc = lookupConstraints( c); if ( cc.gridx == cc.RELATIVE) { if ( lcc == null ) x1 = 0; else if ( lcc.gridwidth > 0 ) x1 += lcc.gridwidth; else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -