我正在使用GridLayout
,这就是我希望网格看起来像:
1 9
2 10
3 11
4 12
5 13
6 14
7 15
8 16
根据此https://docs.oracle.com/javase/7/docs/api/java/awt/GridLayout.html,这是ComponentOrientation
的问题。
ComponentOrientation
本身就是一个类,可以传递给setComponentOrientation
类的Container
方法,但似乎没有构造函数,也没有办法改变它的字段。
答案 0 :(得分:0)
实际上,您可以通过扩展内置的GridLayout
并覆盖单个方法来轻松实现此目的。我已经评论了实际上与父类实现不同的两行。其余注释是原始课程的一部分。
public class VerticalGridLayout
extends GridLayout
{
/**
* Creates a grid layout with a default of one column per component, in a single row.
*/
public VerticalGridLayout()
{
super();
}
/**
* Creates a grid layout with the specified number of rows and columns. All components in the
* layout are given equal size.
* <p>
* One, but not both, of <code>rows</code> and <code>cols</code> can be zero, which means that any
* number of objects can be placed in a row or in a column.
*
* @param rows the rows, with the value zero meaning any number of rows.
* @param cols the columns, with the value zero meaning any number of columns.
*/
public VerticalGridLayout( int rows, int cols )
{
super( rows, cols );
}
/**
* Creates a grid layout with the specified number of rows and columns. All components in the
* layout are given equal size.
* <p>
* In addition, the horizontal and vertical gaps are set to the specified values. Horizontal gaps
* are placed between each of the columns. Vertical gaps are placed between each of the rows.
* <p>
* One, but not both, of <code>rows</code> and <code>cols</code> can be zero, which means that any
* number of objects can be placed in a row or in a column.
* <p>
* All <code>GridLayout</code> constructors defer to this one.
*
* @param rows the rows, with the value zero meaning any number of rows
* @param cols the columns, with the value zero meaning any number of columns
* @param hgap the horizontal gap
* @param vgap the vertical gap
* @exception IllegalArgumentException if the value of both <code>rows</code> and
* <code>cols</code> is set to zero
*/
public VerticalGridLayout( int rows, int cols, int hgap, int vgap )
{
super( rows, cols, hgap, vgap );
}
@Override
public void layoutContainer( Container parent )
{
synchronized ( parent.getTreeLock() )
{
Insets insets = parent.getInsets();
int ncomponents = parent.getComponentCount();
int nrows = getRows();
int ncols = getColumns();
boolean ltr = parent.getComponentOrientation().isLeftToRight();
if ( ncomponents == 0 )
{
return;
}
if ( nrows > 0 )
{
ncols = (ncomponents + nrows - 1) / nrows;
}
else
{
nrows = (ncomponents + ncols - 1) / ncols;
}
// 4370316. To position components in the center we should:
// 1. get an amount of extra space within Container
// 2. incorporate half of that value to the left/top position
// Note that we use trancating division for widthOnComponent
// The reminder goes to extraWidthAvailable
int totalGapsWidth = (ncols - 1) * getHgap();
int widthWOInsets = parent.getWidth() - (insets.left + insets.right);
int widthOnComponent = (widthWOInsets - totalGapsWidth) / ncols;
int extraWidthAvailable = (widthWOInsets - (widthOnComponent * ncols + totalGapsWidth)) / 2;
int totalGapsHeight = (nrows - 1) * getVgap();
int heightWOInsets = parent.getHeight() - (insets.top + insets.bottom);
int heightOnComponent = (heightWOInsets - totalGapsHeight) / nrows;
int extraHeightAvailable = (heightWOInsets - (heightOnComponent * nrows + totalGapsHeight)) / 2;
if ( ltr )
{
for ( int c = 0, x = insets.left + extraWidthAvailable; c < ncols; c++, x += widthOnComponent
+ getHgap() )
{
for ( int r = 0, y = insets.top + extraHeightAvailable; r < nrows; r++, y += heightOnComponent
+ getVgap() )
{
int i = c * nrows + r; // In the horizontal version, this is: r * ncols + c
if ( i < ncomponents )
{
parent.getComponent( i ).setBounds( x, y, widthOnComponent, heightOnComponent );
}
}
}
}
else
{
for ( int c = 0, x = (parent.getWidth() - insets.right - widthOnComponent)
- extraWidthAvailable; c < ncols; c++, x -= widthOnComponent + getHgap() )
{
for ( int r = 0, y = insets.top + extraHeightAvailable; r < nrows; r++, y += heightOnComponent
+ getVgap() )
{
int i = c * nrows + r; // In the horizontal version, this is: r * ncols + c
if ( i < ncomponents )
{
parent.getComponent( i ).setBounds( x, y, widthOnComponent, heightOnComponent );
}
}
}
}
}
}
}