📄 tablelayout.java
字号:
package net.sf.hibern8ide.swing;import java.awt.*;import java.util.*;/** * TableLayout is a layout manager that arranges components in rows and columns * like a spreadsheet. TableLayout allows each row or column to be a different * size. A row or column can be given an absolute size in pixels, a percentage * of the available space, or it can grow and shrink to fill the remaining space * after other rows and columns have been resized. * * <p>Using spreadsheet terminology, a cell is the intersection of a row and * column. Cells have finite, non-negative sizes measured in pixels. The * dimensions of a cell depend solely upon the dimensions of its row and column. * </p> * * <p>A component occupies a rectangular group of one or more cells. If the * component occupies more than one cell, the component is resized to fit * perfectly in the rectangular region of cells. If the component occupies a * single cell, it can be aligned in four ways within that cell.</p> * * <p>A single cell component can be stretched horizontally to fit the cell * (full justification), or it can be placed in the center of the cell. The * component could also be left justified or right justified. Similarly, the * component can be full, center, top, or bottom justified in the vertical.</p> * * <pre> * public static void main (String args[]) * { * // Create a frame * Frame frame = new Frame("Example of TableLayout"); * frame.setBounds (100, 100, 300, 300); * <spc> * // Create a TableLayout for the frame * double border = 10; * double size[][] = * {{border, 0.10, 20, TableLayout.FILL, 20, 0.20, border}, // Columns * {border, 0.20, 20, TableLayout.FILL, 20, 0.20, border}}; // Rows * <spc> * frame.setLayout (new TableLayout(size)); * <spc> * // Create some buttons * String label[] = {"Top", "Bottom", "Left", "Right", "Center", "Overlap"}; * Button button[] = new Button[label.length]; * <spc> * for (int i = 0; i < label.length; i++) * button[i] = new Button(label[i]); * <spc> * // Add buttons * frame.add (button[0], "1, 1, 5, 1"); // Top * frame.add (button[1], "1, 5, 5, 5"); // Bottom * frame.add (button[2], "1, 3 "); // Left * frame.add (button[3], "5, 3 "); // Right * frame.add (button[4], "3, 3, c, c"); // Center * frame.add (button[5], "3, 3, 3, 5"); // Overlap * <spc> * // Allow user to close the window to terminate the program * frame.addWindowListener * (new WindowListener() * { * public void windowClosing (WindowEvent e) * { * System.exit (0); * } * <spc> * public void windowOpened (WindowEvent e) {} * public void windowClosed (WindowEvent e) {} * public void windowIconified (WindowEvent e) {} * public void windowDeiconified (WindowEvent e) {} * public void windowActivated (WindowEvent e) {} * public void windowDeactivated (WindowEvent e) {} * } * ); * <spc> * // Show frame * frame.show(); * } * </pre> * * @author Daniel E. Barbalace */public class TableLayout implements java.awt.LayoutManager2, java.io.Serializable, TableLayoutConstants { /** Default row/column size */ protected static final double defaultSize[][] = {{}, {}}; /** Widths of columns expressed in absolute and relative terms */ protected double columnSpec[]; /** Heights of rows expressed in absolute and relative terms */ protected double rowSpec[]; /** Widths of columns in pixels */ protected int columnSize[]; /** Heights of rows in pixels */ protected int rowSize[]; /** Offsets of columns in pixels. The left boarder of column n is at columnOffset[n] and the right boarder is at columnOffset[n + 1] for all columns including the last one. columnOffset.length = columnSize.length + 1 */ protected int columnOffset[]; /** Offsets of rows in pixels. The left boarder of row n is at rowOffset[n] and the right boarder is at rowOffset[n + 1] for all rows including the last one. rowOffset.length = rowSize.length + 1 */ protected int rowOffset[]; /** List of components and their sizes */ protected LinkedList list; /** Indicates whether or not the size of the cells are known for the last known size of the container. If dirty is true or the container has been resized, the cell sizes must be recalculated using calculateSize. */ protected boolean dirty; /** Previous known width of the container */ protected int oldWidth; /** Previous known height of the container */ protected int oldHeight; //****************************************************************************** //** Constructors *** //****************************************************************************** /** * Constructs an instance of TableLayout. This TableLayout will have one row * and one column. */ public TableLayout () { this (defaultSize); } /** * Constructs an instance of TableLayout. * * @param size widths of columns and heights of rows in the format, * {{col0, col1, col2, ..., colN}, {row0, row1, row2, ..., rowM}} * If this parameter is invalid, the TableLayout will have * exactly one row and one column. */ public TableLayout (double size[][]) { // Make sure rows and columns and nothing else is specified if ((size != null) && (size.length == 2)) { // Get the rows and columns double tempCol[] = size[0]; double tempRow[] = size[1]; // Create new rows and columns columnSpec = new double[tempCol.length]; rowSpec = new double[tempRow.length]; // Copy rows and columns System.arraycopy (tempCol, 0, columnSpec, 0, columnSpec.length); System.arraycopy (tempRow, 0, rowSpec, 0, rowSpec.length); // Make sure rows and columns are valid for (int counter = 0; counter < columnSpec.length; counter++) if ((columnSpec[counter] < 0.0) && (columnSpec[counter] != FILL) && (columnSpec[counter] != PREFERRED) && (columnSpec[counter] != MINIMUM)) { columnSpec[counter] = 0.0; } for (int counter = 0; counter < rowSpec.length; counter++) if ((rowSpec[counter] < 0.0) && (rowSpec[counter] != FILL) && (rowSpec[counter] != PREFERRED) && (rowSpec[counter] != MINIMUM)) { rowSpec[counter] = 0.0; } } else { double tempCol[] = {FILL}; double tempRow[] = {FILL}; setColumn (tempCol); setRow (tempRow); } // Create an empty list of components list = new LinkedList(); // Indicate that the cell sizes are not known dirty = true; } //****************************************************************************** //** Get/Set methods *** //****************************************************************************** /** * Gets the constraints of a given component. * * @param component desired component * * @return If the given component is found, the constraints associated with * that component. If the given component is null or is not found, * null is returned. */ public TableLayoutConstraints getConstraints (Component component) { ListIterator iterator = list.listIterator(0); while (iterator.hasNext()) { Entry entry = (Entry) iterator.next(); if (entry.component == component) return new TableLayoutConstraints (entry.col1, entry.row1, entry.col2, entry.row2, entry.hAlign, entry.vAlign); } return null; } /** * Sets the constraints of a given component. * * @param component desired component. This parameter cannot be null. * @param constraint new set of constraints. This parameter cannot be null. * * @return If the given component is found, the constraints associated with * that component. If the given component is null or is not found, * null is returned. */ public void setConstraints (Component component, TableLayoutConstraints constraint) { // Check parameters if (component == null) throw new IllegalArgumentException ("Parameter component cannot be null."); else if (constraint == null) throw new IllegalArgumentException ("Parameter constraint cannot be null."); // Find and update constraints for the given component ListIterator iterator = list.listIterator(0); while (iterator.hasNext()) { Entry entry = (Entry) iterator.next(); if (entry.component == component) iterator.set (new Entry(component, constraint)); } } /** * Adjusts the number and sizes of rows in this layout. After calling this * method, the caller should request this layout manager to perform the * layout. This can be done with the following code: * * <pre> * layout.layoutContainer(container); * container.repaint(); * </pre> * * or * * <pre> * window.pack() * </pre> * * If this is not done, the changes in the layout will not be seen until the * container is resized. * * @param column heights of each of the columns * * @see getColumn */ public void setColumn (double column[]) { // Copy columns columnSpec = new double[column.length]; System.arraycopy (column, 0, columnSpec, 0, columnSpec.length); // Make sure columns are valid for (int counter = 0; counter < columnSpec.length; counter++) if ((columnSpec[counter] < 0.0) && (columnSpec[counter] != FILL) && (columnSpec[counter] != PREFERRED) && (columnSpec[counter] != MINIMUM)) { columnSpec[counter] = 0.0; } // Indicate that the cell sizes are not known dirty = true; } /** * Adjusts the number and sizes of rows in this layout. After calling this * method, the caller should request this layout manager to perform the * layout. This can be done with the following code: * * <code> * layout.layoutContainer(container); * container.repaint(); * </code> * * or * * <pre> * window.pack() * </pre> * * If this is not done, the changes in the layout will not be seen until the * container is resized. * * @param row widths of each of the rows. This parameter cannot be null. * * @see getRow */ public void setRow (double row[]) { // Copy rows rowSpec = new double[row.length]; System.arraycopy (row, 0, rowSpec, 0, rowSpec.length); // Make sure rows are valid for (int counter = 0; counter < rowSpec.length; counter++) if ((rowSpec[counter] < 0.0) && (rowSpec[counter] != FILL) && (rowSpec[counter] != PREFERRED) && (rowSpec[counter] != MINIMUM)) { rowSpec[counter] = 0.0; } // Indicate that the cell sizes are not known dirty = true; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -