📄 tablelayout.java
字号:
entry.col1--; // Is the second column to the right of the new column if (entry.col2 >= i) // Move second column entry.col2--; } // Indicate that the cell sizes are not known dirty = true; } /** * Deletes a row in this layout. All components below the deletion point are * moved up one row. The container will need to be laid out after this method * returns. See <code>setRow</code>. There must be at least two rows in order * to delete a row. * * @param i zero-based index of column to delete * * @see setRow * @see deleteRow */ public void deleteRow (int i) { // Make sure position is valid if ((i < 0) || (i >= rowSpec.length)) throw new IllegalArgumentException ("Parameter i is invalid. i = " + i + ". Valid range is [0, " + (rowSpec.length - 1) + "]."); // Copy rows double row[] = new double[rowSpec.length - 1]; System.arraycopy (rowSpec, 0, row, 0, i); System.arraycopy (rowSpec, i + 1, row, i, rowSpec.length - i - 1); // Delete row rowSpec = row; // Move all components that are to below the row deleted ListIterator iterator = list.listIterator(0); while (iterator.hasNext()) { // Get next entry Entry entry = (Entry) iterator.next(); // Is the first row below the new row if (entry.row1 >= i) // Move first row entry.row1--; // Is the second row below the new row if (entry.row2 >= i) // Move second row entry.row2--; } // Indicate that the cell sizes are not known dirty = true; } //****************************************************************************** //** Misc methods *** //****************************************************************************** /** * Converts this TableLayout to a string. * * @return a string representing the columns and row sizes in the form * "{{col0, col1, col2, ..., colN}, {row0, row1, row2, ..., rowM}}" */ public String toString () { int counter; String value = "TableLayout {{"; if (columnSpec.length > 0) { for (counter = 0; counter < columnSpec.length - 1; counter++) value += columnSpec[counter] + ", "; value += columnSpec[columnSpec.length - 1] + "}, {"; } else value += "}, {"; if (rowSpec.length > 0) { for (counter = 0; counter < rowSpec.length - 1; counter++) value += rowSpec[counter] + ", "; value += rowSpec[rowSpec.length - 1] + "}}"; } else value += "}}"; return value; } /** * Draws a grid on the given container. This is useful for seeing where the * rows and columns go. In the container's paint method, call this method. * * @param container container using this TableLayout * @param g graphics content of container (can be offscreen) */ public void drawGrid (Container container, Graphics g) { int counter; // Counting variable; // Calculate the sizes of the rows and columns Dimension d = container.getSize(); if (dirty || (d.width != oldWidth) || (d.height != oldHeight)) calculateSize (container); // Initialize y int y = 0; for (int row = 0; row < rowSize.length; row++) { // Initialize x int x = 0; for (int column = 0; column < columnSize.length; column++) { // Use a random color to make things easy to see Color color = new Color((int) (Math.random() * 0xFFFFFFL)); g.setColor (color); // Draw the cell as a solid rectangle g.fillRect (x, y, columnSize[column], rowSize[row]); // Increment x x += columnSize[column]; } // Increment y y += rowSize[row]; } } /** * Determines whether or not there are any hidden components. A hidden * component is one that will not be shown with this layout's current * configuration. Such a component is, at least partly, in an invalid row * or column. For example, on a table with five rows, row -1 and row 5 are both * invalid. Valid rows are 0 through 4, inclusively. * * @return True, if there are any hidden components. False, otherwise. * * @see overlapping */ public boolean hidden () { // Assume no components are hidden boolean hidden = false; // Check all components ListIterator iterator = list.listIterator(0); while (iterator.hasNext()) { // Get next entry Entry entry = (Entry) iterator.next(); // Is this component valid if ((entry.row1 < 0) || (entry.col1 < 0) || (entry.row2 > rowSpec.length) || (entry.col2 > columnSpec.length)) { hidden = true; break; } } return hidden; } /** * Determines whether or not there are any overlapping components. Two * components overlap if they cover at least one common cell. * * @return True, if there are any overlapping components. False, otherwise. * * @see hidden */ public boolean overlapping () { // Count contraints int numEntry = list.size(); // If there are no components, they can't be overlapping if (numEntry == 0) return false; // Assume no components are overlapping boolean overlapping = false; // Put entries in an array Entry entry[] = (Entry[]) list.toArray(new Entry[numEntry]); // Check all components for (int knowUnique = 1; knowUnique < numEntry; knowUnique++) for (int checking = knowUnique - 1; checking >= 0; checking--) if ( ( (entry[checking].col1 >= entry[knowUnique].col1) && (entry[checking].col1 <= entry[knowUnique].col2) && (entry[checking].row1 >= entry[knowUnique].row1) && (entry[checking].row1 <= entry[knowUnique].row2) ) || ( (entry[checking].col2 >= entry[knowUnique].col1) && (entry[checking].col2 <= entry[knowUnique].col2) && (entry[checking].row2 >= entry[knowUnique].row1) && (entry[checking].row2 <= entry[knowUnique].row2) ) ) { overlapping = true; break; } return overlapping; } /** * Calculates the sizes of the rows and columns based on the absolute and * relative sizes specified in <code>rowSpec</code> and <code>columnSpec</code> * and the size of the container. The result is stored in <code>rowSize</code> * and <code>columnSize</code>. * * @param container container using this TableLayout */ protected void calculateSize (Container container) { int counter; // Counting variable; // Get number of rows and columns int numColumn = columnSpec.length; int numRow = rowSpec.length; // Create array to hold actual sizes in pixels columnSize = new int[numColumn]; rowSize = new int[numRow]; // Get the container's insets Insets inset = container.getInsets(); // Get the size of the container's available space Dimension d = container.getSize(); int totalWidth = d.width - inset.left - inset.right; int totalHeight = d.height - inset.top - inset.bottom; // Initially, the available space is the total space int availableWidth = totalWidth; int availableHeight = totalHeight; // Assign absolute widths; this reduces available width for (counter = 0; counter < numColumn; counter++) // Is the current column an absolue size if ((columnSpec[counter] >= 1.0) || (columnSpec[counter] == 0.0)) { // Assign absolute width columnSize[counter] = (int) (columnSpec[counter] + 0.5); // Reduce available width availableWidth -= columnSize[counter]; } // Assign absolute heights; this reduces available height for (counter = 0; counter < numRow; counter++) // Is the current column an absolue size if ((rowSpec[counter] >= 1.0) || (rowSpec[counter] == 0.0)) { // Assign absolute width rowSize[counter] = (int) (rowSpec[counter] + 0.5); // Reduce available width availableHeight -= rowSize[counter]; } // Assign preferred and minimum widths; this reduces available width. // Assignment of preferred/minimum with is like assignment of absolute // widths except that each column must determine the maximum // preferred/minimum width of the components that are completely contained // within the column. for (counter = 0; counter < numColumn; counter++) // Is the current column a preferred size if ((columnSpec[counter] == PREFERRED) || (columnSpec[counter] == MINIMUM)) { // Assume a maximum width of zero int maxWidth = 0; // Find maximum preferred width of all components completely // contained within this column ListIterator iterator = list.listIterator(0); while (iterator.hasNext()) { Entry entry = (Entry) iterator.next(); if ((entry.col1 == counter) && (entry.col2 == counter)) { Dimension p = (columnSpec[counter] == PREFERRED) ? entry.component.getPreferredSize() : entry.component.getMinimumSize(); int width = (p == null) ? 0 : p.width; if (maxWidth < width) maxWidth = width; } } // Assign preferred width columnSize[counter] = maxWidth; // Reduce available width availableWidth -= maxWidth; } // Assign preferred and minimum heights; this reduces available height. // Assignment of preferred/minimum with is like assignment of absolute // heights except that each row must determine the maximum // preferred/minimum height of the components that are completely contained // within the row. for (counter = 0; counter < numRow; counter++) // Is the current row a preferred size if ((rowSpec[counter] == PREFERRED) || (rowSpec[counter] == MINIMUM)) { // Assume a maximum height of zero int maxHeight = 0; // Find maximum preferred height of all components completely
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -