更改 column/row 节点在 GridPane 中的索引
Change column/row index of node in GridPane
我将 GridPane 用作 n x m 矩阵,我需要能够在其中调整水平条的大小。
我有一个 Circle 作为 GridPane 的子项添加到其中,我有一个动作侦听器,它允许我调整水平条的大小,并且它工作正常。
我正在使用
int col = GridPane.getColumnIndex(dragAnchor);
获取锚所在的索引,然后调用:
GridPane.setColumnIndex(dragAnchor, newCol);
(其中 newCol 是返回值 +/- 1,具体取决于我是增大还是减小尺寸)。
当我这样做时,Circle 节点不会在 gridPane 上移动,返回的 col 也是正确的位置。是否必须执行其他操作才能移动已添加到 GridPane 的节点?[=13=]
它按预期工作,没有问题。由于 gridpane 的空列(单元格)宽度为 0,您可能无法在应用程序中观察到 "moving"。要以可视方式调试,请将 gridLinesVisible 设置为 true 并添加一些 row/column 约束。请参阅下面的示例:
@Override
public void start( Stage stage )
{
GridPane gp = new GridPane();
Label l = new Label( "before" );
Button b = new Button( "move" );
b.setOnAction( ( e ) ->
{
int i = GridPane.getColumnIndex( l );
System.out.println( "i = " + i );
l.setText( "after" );
GridPane.setColumnIndex( l, 2 );
} );
gp.add( l, 0, 0 );
gp.add( b, 1, 1 );
gp.setGridLinesVisible( true );
gp.getColumnConstraints().addAll( new ColumnConstraints( 70 ), new ColumnConstraints( 70 ), new ColumnConstraints( 70 ) );
final Scene scene = new Scene( gp, 400, 300 );
stage.setScene( scene );
stage.show();
}
我将 GridPane 用作 n x m 矩阵,我需要能够在其中调整水平条的大小。
我有一个 Circle 作为 GridPane 的子项添加到其中,我有一个动作侦听器,它允许我调整水平条的大小,并且它工作正常。
我正在使用
int col = GridPane.getColumnIndex(dragAnchor);
获取锚所在的索引,然后调用:
GridPane.setColumnIndex(dragAnchor, newCol);
(其中 newCol 是返回值 +/- 1,具体取决于我是增大还是减小尺寸)。
当我这样做时,Circle 节点不会在 gridPane 上移动,返回的 col 也是正确的位置。是否必须执行其他操作才能移动已添加到 GridPane 的节点?[=13=]
它按预期工作,没有问题。由于 gridpane 的空列(单元格)宽度为 0,您可能无法在应用程序中观察到 "moving"。要以可视方式调试,请将 gridLinesVisible 设置为 true 并添加一些 row/column 约束。请参阅下面的示例:
@Override
public void start( Stage stage )
{
GridPane gp = new GridPane();
Label l = new Label( "before" );
Button b = new Button( "move" );
b.setOnAction( ( e ) ->
{
int i = GridPane.getColumnIndex( l );
System.out.println( "i = " + i );
l.setText( "after" );
GridPane.setColumnIndex( l, 2 );
} );
gp.add( l, 0, 0 );
gp.add( b, 1, 1 );
gp.setGridLinesVisible( true );
gp.getColumnConstraints().addAll( new ColumnConstraints( 70 ), new ColumnConstraints( 70 ), new ColumnConstraints( 70 ) );
final Scene scene = new Scene( gp, 400, 300 );
stage.setScene( scene );
stage.show();
}