📄 gridbaglayout.java
字号:
/* GridBagLayout - Layout manager for components according to GridBagConstraints Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package java.awt;import java.io.Serializable;import java.util.ArrayList;import java.util.HashMap;import java.util.Hashtable;/** * @author Michael Koch (konqueror@gmx.de) * @author Jeroen Frijters (jeroen@frijters.net) */public class GridBagLayout implements Serializable, LayoutManager2{ private static final long serialVersionUID = 8838754796412211005L; protected static final int MINSIZE = 1; protected static final int PREFERREDSIZE = 2; protected static final int MAXGRIDSIZE = 512; // comptable remembers the original contraints given to us. // internalcomptable is used to keep track of modified constraint values // that we calculate, particularly when we are given RELATIVE and // REMAINDER constraints. // Constraints kept in comptable are never modified, and constraints // kept in internalcomptable can be modified internally only. protected Hashtable comptable; private Hashtable internalcomptable; protected GridBagLayoutInfo layoutInfo; protected GridBagConstraints defaultConstraints; public double[] columnWeights; public int[] columnWidths; public double[] rowWeights; public int[] rowHeights; public GridBagLayout () { this.comptable = new Hashtable(); this.internalcomptable = new Hashtable(); this.defaultConstraints= new GridBagConstraints(); } /** * Helper method to calc the sum of a range of elements in an int array. */ private int sumIntArray (int[] array, int upto) { int result = 0; for (int i = 0; i < upto; i++) result += array [i]; return result; } /** * Helper method to calc the sum of all elements in an int array. */ private int sumIntArray (int[] array) { return sumIntArray(array, array.length); } /** * Helper method to calc the sum of all elements in an double array. */ private double sumDoubleArray (double[] array) { double result = 0; for (int i = 0; i < array.length; i++) result += array [i]; return result; } public void addLayoutComponent (String name, Component component) { // do nothing here. } public void removeLayoutComponent (Component component) { // do nothing here } public void addLayoutComponent (Component component, Object constraints) { if (constraints == null) return; if (!(constraints instanceof GridBagConstraints)) throw new IllegalArgumentException("constraints " + constraints + " are not an instance of GridBagConstraints"); setConstraints (component, (GridBagConstraints) constraints); } public Dimension preferredLayoutSize (Container parent) { if (parent == null) return new Dimension (0, 0); GridBagLayoutInfo li = getLayoutInfo (parent, PREFERREDSIZE); return getMinSize (parent, li); } public Dimension minimumLayoutSize (Container parent) { if (parent == null) return new Dimension (0, 0); GridBagLayoutInfo li = getLayoutInfo (parent, MINSIZE); return getMinSize (parent, li); } public Dimension maximumLayoutSize (Container target) { return new Dimension (Integer.MAX_VALUE, Integer.MAX_VALUE); } public void layoutContainer (Container parent) { arrangeGrid (parent); } public float getLayoutAlignmentX (Container target) { return Component.CENTER_ALIGNMENT; } public float getLayoutAlignmentY (Container target) { return Component.CENTER_ALIGNMENT; } public void invalidateLayout (Container target) { this.layoutInfo = null; } public void setConstraints (Component component, GridBagConstraints constraints) { GridBagConstraints clone = (GridBagConstraints) constraints.clone(); if (clone.gridx < 0) clone.gridx = GridBagConstraints.RELATIVE; if (clone.gridy < 0) clone.gridy = GridBagConstraints.RELATIVE; if (clone.gridwidth == 0) clone.gridwidth = GridBagConstraints.REMAINDER; else if (clone.gridwidth < 0) clone.gridwidth = 1; if (clone.gridheight == 0) clone.gridheight = GridBagConstraints.REMAINDER; else if (clone.gridheight < 0) clone.gridheight = 1; comptable.put (component, clone); } public GridBagConstraints getConstraints (Component component) { return (GridBagConstraints) (lookupConstraints (component).clone()); } protected GridBagConstraints lookupConstraints (Component component) { GridBagConstraints result = (GridBagConstraints) comptable.get (component); if (result == null) { setConstraints (component, defaultConstraints); result = (GridBagConstraints) comptable.get (component); } return result; } private GridBagConstraints lookupInternalConstraints (Component component) { GridBagConstraints result = (GridBagConstraints) internalcomptable.get (component); if (result == null) { result = (GridBagConstraints) lookupConstraints(component).clone(); internalcomptable.put (component, result); } return result; } /** * @since 1.1 */ public Point getLayoutOrigin () { if (layoutInfo == null) return new Point (0, 0); return new Point (layoutInfo.pos_x, layoutInfo.pos_y); } /** * @since 1.1 */ public int[][] getLayoutDimensions () { int[][] result = new int [2][]; if (layoutInfo == null) { result[0] = new int[0]; result[1] = new int[0]; return result; } result [0] = new int [layoutInfo.cols]; System.arraycopy (layoutInfo.colWidths, 0, result [0], 0, layoutInfo.cols); result [1] = new int [layoutInfo.rows]; System.arraycopy (layoutInfo.rowHeights, 0, result [1], 0, layoutInfo.rows); return result; } public double[][] getLayoutWeights () { double[][] result = new double [2][]; if (layoutInfo == null) { result[0] = new double[0]; result[1] = new double[0]; return result; } result [0] = new double [layoutInfo.cols]; System.arraycopy (layoutInfo.colWeights, 0, result [0], 0, layoutInfo.cols); result [1] = new double [layoutInfo.rows]; System.arraycopy (layoutInfo.rowWeights, 0, result [1], 0, layoutInfo.rows); return result; } /** * @since 1.1 */ public Point location (int x, int y) { if (layoutInfo == null) return new Point (0, 0); int col; int row; int pixel_x = layoutInfo.pos_x; int pixel_y = layoutInfo.pos_y; for (col = 0; col < layoutInfo.cols; col++) { int w = layoutInfo.colWidths [col]; if (x < pixel_x + w) break; pixel_x += w; } for (row = 0; row < layoutInfo.rows; row++) { int h = layoutInfo.rowHeights [row]; if (y < pixel_y + h) break; pixel_y += h; } return new Point (col, row); } /** * Obsolete. */ protected void AdjustForGravity (GridBagConstraints gbc, Rectangle rect) { // FIXME throw new Error ("Not implemented"); } /** * Obsolete. */ protected void ArrangeGrid (Container parent) { Component[] components = parent.getComponents(); if (components.length == 0) return; GridBagLayoutInfo info = getLayoutInfo (parent, PREFERREDSIZE); if (info.cols == 0 && info.rows == 0) return; layoutInfo = info; // DEBUG //dumpLayoutInfo (layoutInfo); for(int i = 0; i < components.length; i++) { Component component = components [i]; // If component is not visible we dont have to care about it. if (!component.isVisible()) continue; GridBagConstraints constraints =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -