📄 gridbaglayout.java
字号:
x1 = -1; } else { x1 = cc.gridx; } if ( cc.gridy == cc.RELATIVE) { if ( lcc == null ) y1 = 0; else if ( lcc.gridheight == cc.REMAINDER ) y1 = -1; else if ( lcc.gridwidth == cc.REMAINDER ) y1 += Math.max( lcc.gridheight, 1); else y1 = -1; } else { y1 = cc.gridy; } if ( x1 == -1 ) { for ( x1=0; defGrid[x1][y1] != null; x1++); } if ( y1 == -1 ) { for ( y1=0; defGrid[x1][y1] != null; y1++); } cx = cc.gridwidth; cy = cc.gridheight; if ( cx == cc.REMAINDER ){ mxr = Math.max( mxr, x1); cx = MAXGRIDSIZE - x1; } else if ( cx == cc.RELATIVE ) { cx = 1; } if ( cy == cc.REMAINDER ) { myr = Math.max( myr, y1); cy = MAXGRIDSIZE - y1; } else if ( cy == cc.RELATIVE) { cy = 1; } for( cy--; cy > -1; cy--) { for ( int icx=cx-1; icx > -1; icx--) { defGrid[x1+icx][y1+cy] = c; } } lcc = cc; } fillGrid( parent, mxr+1, myr+1, mode); }}public float getLayoutAlignmentX( Container parent) { return Component.CENTER_ALIGNMENT;}public float getLayoutAlignmentY( Container parent) { return Component.CENTER_ALIGNMENT;}public int[][] getLayoutDimensions() { return (new int[][]{columnWidths, rowHeights});}public Point getLayoutOrigin() { return offset;}public double[][] getLayoutWeights() { return (new double[][]{columnWeights, rowWeights});}Rectangle gridToPels( Rectangle r ) { int x, y, xp, yp, wp, hp; for ( xp=0, x=0; x<r.x; x++) { xp += columnWidths[x]; } for ( wp=0; x<r.x+r.width; x++) { wp += columnWidths[x]; } for ( yp=0, y=0; y<r.y; y++) { yp += rowHeights[y]; } for ( hp=0; y<r.y+r.height; y++) { hp += rowHeights[y]; } r.setBounds( xp, yp, wp, hp); return r;}public void invalidateLayout( Container parent) {}public void layoutContainer( Container parent) { Insets pin = parent.getInsets(); Rectangle cRect = new Rectangle(); int nc = parent.getComponentCount(); getGrid( parent, ACTUALSIZE); for ( int idx=0; idx<nc; idx++) { Component c = parent.getComponent( idx); GridBagConstraints cc = lookupConstraints( c); Insets in = cc.insets; cRect.setBounds( cc.gridx, cc.gridy, cc.gridwidth, cc.gridheight); gridToPels( cRect); Dimension cd = c.getPreferredSize(); int cx = cRect.x + pin.left + offset.x; int cy = cRect.y + pin.top + offset.y; int cw = Math.min( cd.width, cRect.width); int ch = Math.min( cd.height, cRect.height); switch ( cc.fill ) { case GridBagConstraints.BOTH: cx += in.left + cc.ipadx; cy += in.top + cc.ipady; cw = cRect.width - (in.left + in.right + 2 * cc.ipadx); ch = cRect.height - (in.top + in.bottom + 2 * cc.ipady); break; case GridBagConstraints.HORIZONTAL: cx += in.left + cc.ipadx; cw = cRect.width - (in.left + in.right + 2 * cc.ipadx); switch ( cc.anchor ) { case GridBagConstraints.WEST: case GridBagConstraints.CENTER: case GridBagConstraints.EAST: default: cy += (cRect.height - ch) / 2; break; case GridBagConstraints.NORTH: case GridBagConstraints.NORTHEAST: case GridBagConstraints.NORTHWEST: cy += in.top + cc.ipady; break; case GridBagConstraints.SOUTHEAST: case GridBagConstraints.SOUTH: case GridBagConstraints.SOUTHWEST: cy += cRect.height - (cd.height + in.bottom + cc.ipady); break; } break; case GridBagConstraints.VERTICAL: cy += in.top + cc.ipady; ch = cRect.height - (in.top + in.bottom + 2 * cc.ipady); switch ( cc.anchor ) { case GridBagConstraints.NORTH: case GridBagConstraints.CENTER: case GridBagConstraints.SOUTH: default: cx += (cRect.width - cw) / 2; break; case GridBagConstraints.NORTHWEST: case GridBagConstraints.WEST: case GridBagConstraints.SOUTHWEST: cx += in.left + cc.ipadx; break; case GridBagConstraints.NORTHEAST: case GridBagConstraints.SOUTHEAST: case GridBagConstraints.EAST: cx += cRect.width - (cw + in.right + cc.ipadx); break; } break; case GridBagConstraints.NONE: default: switch ( cc.anchor ) { case GridBagConstraints.NORTH: cy += in.top + cc.ipady; cx += (cRect.width - cw) / 2; break; case GridBagConstraints.NORTHEAST: cy += in.top + cc.ipady; cx += cRect.width - (cw + in.right + cc.ipadx); break; case GridBagConstraints.EAST: cy += (cRect.height - ch) / 2; cx += cRect.width - (cw + in.right + cc.ipadx); break; case GridBagConstraints.SOUTHEAST: cy += cRect.height - (ch + in.bottom + cc.ipady); cx += cRect.width - (cw + in.right + cc.ipadx); break; case GridBagConstraints.SOUTH: cy += cRect.height - (ch + in.bottom + cc.ipady); cx += (cRect.width - cw) / 2; break; case GridBagConstraints.SOUTHWEST: cy += cRect.height - (ch + in.bottom + cc.ipady); cx += in.left + cc.ipadx; break; case GridBagConstraints.WEST: cy += (cRect.height - ch) / 2; cx += in.left + cc.ipadx; break; case GridBagConstraints.NORTHWEST: cy += in.top + cc.ipady; cx += in.left + cc.ipadx; break; case GridBagConstraints.CENTER: default: cy += (cRect.height - ch) / 2; cx += (cRect.width - cw) / 2; break; } } c.setBounds( cx, cy, cw, ch); }}public Point location( int x, int y) { int x0 = offset.x; int y0 = offset.y; int ix, iy; for ( ix = 0; ix < columnWidths.length; ix++){ x0 += columnWidths[ix]; if ( x < x0 ) { break; } } for ( iy = 0; ix < rowHeights.length; iy++){ y0 += rowHeights[iy]; if ( y < y0 ) { break; } } return new Point( ix, iy);}protected GridBagConstraints lookupConstraints(Component c) { GridBagConstraints cons = (GridBagConstraints)comptable.get(c); if (cons == null) { cons = defaultConstraints; } return (cons);}public Dimension maximumLayoutSize( Container parent) { return (new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));}public Dimension minimumLayoutSize( Container parent) { // Don't use the insets field directly, since getInsets() might be resolved (e.g. in // swing popups). Remember BorderLayout isVisible? It's spelled c o n s i s t e n c y Insets in = parent.getInsets(); getGrid( parent, MINSIZE); return new Dimension( sumWidths()+in.left+in.right, sumHeights()+in.top+in.bottom );}public Dimension preferredLayoutSize( Container parent) { Insets in = parent.getInsets(); getGrid( parent, PREFERREDSIZE); return new Dimension( sumWidths()+in.left+in.right, sumHeights()+in.top+in.bottom );}public void removeLayoutComponent( Component c) {}public void setConstraints( Component comp, GridBagConstraints cons) { // System.out.println( "set: " + this + " " + comp.getClass().getName() + " " + cons); comptable.put(comp, (GridBagConstraints)cons.clone());}int sumHeights() { int h = 0; for ( int i=0; i<rowHeights.length; i++) { h += rowHeights[i]; } return h;}int sumWidths() { int w = 0; for ( int i=0; i<columnWidths.length; i++) { w += columnWidths[i]; } return w;}public String toString() { // return Integer.toHexString( hashCode() ); int w = (columnWidths != null) ? columnWidths.length : 0; int h = (rowHeights != null) ? rowHeights.length : 0; return ("GridBagLayout columns: " + w + ",rows: " + h);}void updateConstraints( Container parent) { // convert relative and remainder constraints // to real values int mx = columnWidths.length; int my = rowHeights.length; int nc = parent.getComponentCount(); for ( int idx=0; idx<nc; idx++) { Component c = parent.getComponent( idx); GridBagConstraints cc = lookupConstraints( c); for ( int y=0; y<my; y++) { for ( int x=0; x<mx; x++) { if ( c == defGrid[x][y] ) { cc.gridx = x; cc.gridy = y; while ( (++x<mx) && (defGrid[x][y] == c) ); while ( (++y<my) && (defGrid[x-1][y] == c) ); cc.gridwidth = x-cc.gridx; cc.gridheight = y-cc.gridy; y = my; break; } } } }}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -