JavaFX将旋转对象的大小调整为GridPane单元格

时间:2016-02-27 21:32:14

标签: javafx-8

在JavaFX中使用setRotate()旋转节点后,我遇到了节点(GridPane)方向的问题。当我旋转节点并将其放在GridPane的单元格中时,我希望旋转的节点适合单元格内部,并且还可以使用单元格调整大小。我添加了一些示例代码,以向您展示我希望得到的结果。

public class MonopolyApp extends Application {
    @Override
    public void start(Stage primaryStage) {
        //Construction of the grid
        GridPane square = new GridPane();
        square.setGridLinesVisible(true);
        final int heightPercent = 14;
        final int widthPercent = 8;

        RowConstraints rowsEdge = new RowConstraints();
        rowsEdge.setPercentHeight(heightPercent);
        RowConstraints rowsMid = new RowConstraints();
        rowsMid.setPercentHeight(widthPercent);
        ColumnConstraints colEdge = new ColumnConstraints();
        colEdge.setPercentWidth(heightPercent);
        ColumnConstraints colMid = new ColumnConstraints();
        colMid.setPercentWidth(widthPercent);

        square.getColumnConstraints().addAll(colEdge, colMid,
                colMid, colMid, colMid, colMid, colMid, colMid, colMid, colMid, colEdge);
        square.getRowConstraints().addAll(rowsEdge, rowsMid,
                rowsMid,rowsMid,rowsMid,rowsMid,rowsMid,rowsMid, rowsMid,rowsMid, rowsEdge);

        GridPane wrongSuare = makeMonopolySquare();
        square.add(wrongSuare, 0, 4);
        wrongSuare.setRotate(90);

        GridPane rightSquare = makeMonopolySquare();
        square.add(rightSquare, 1, 10);

        Scene s = new Scene(square);
        primaryStage.setHeight(500);
        primaryStage.setWidth(500);
        primaryStage.setScene(s);
        primaryStage.show();

    }

    public static void main(String[] args) {
        Application.launch(args);
    }

    private GridPane makeMonopolySquare(){
        GridPane monopolySquare = new GridPane();


        RowConstraints top = new RowConstraints();
        top.setPercentHeight(20);

        RowConstraints bottom = new RowConstraints();
        bottom.setPercentHeight(80);

        ColumnConstraints  c = new ColumnConstraints();
        c.setPercentWidth(100);

        monopolySquare.getRowConstraints().addAll(top,bottom);
        monopolySquare.getColumnConstraints().addAll(c);
        bottom.setValignment(VPos.CENTER);

        monopolySquare.setGridLinesVisible(true);

        Label name = new Label("name");
        Pane colorPane = new Pane();
        colorPane.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
        colorPane.setBackground(new Background( new BackgroundFill(Color.BLUE, null, null)));
        GridPane.setMargin(colorPane, new Insets(1));

        monopolySquare.add(colorPane,0,0);
        monopolySquare.add(name, 0, 1);

        return monopolySquare;
    }
}

如果您运行代码,您将看到舞台底部的GridPane完全适合它的单元格。但是旋转的GridPane没有。我还会添加一张图片,向您展示我的问题:

enter image description here

有谁知道如何解决这个问题?我知道我可以把它放在一个组中,但将它放在一个组中的问题是该组不会调整大小到GridPane的单元格。

1 个答案:

答案 0 :(得分:1)

嗯,这是一个艰难的。没有想到最佳解决方案,但如果我处于你的位置,我会尽量保持在布局的限制内(不要使用像旋转那样的低级操作)。

例如,为每个行/列构建不同的布局单元格:

private static Node makeMonopolySquare( final Layout layout )
  {
    final GridPane monopolySquare = new GridPane();
    monopolySquare.setGridLinesVisible( true );
    final Label label = new Label( "name" );
    final StackPane name = new StackPane( label );
    final Pane colorPane = new Pane();
    GridPane.setMargin( colorPane, new Insets( 1 ) );
    colorPane.setMaxSize( Double.MAX_VALUE, Double.MAX_VALUE );
    colorPane.setBackground( new Background( new BackgroundFill( Color.BLUE, null, null ) ) );
    final RowConstraints top = new RowConstraints();
    final RowConstraints bottom = new RowConstraints();
    //    bottom.setValignment( VPos.CENTER );
    //    top.setValignment( VPos.CENTER );
    final ColumnConstraints c = new ColumnConstraints();
    c.setPercentWidth( 100 );
    final ColumnConstraints right = new ColumnConstraints();
    //    right.setHalignment( HPos.CENTER );
    final ColumnConstraints left = new ColumnConstraints();
    //    left.setHalignment( HPos.CENTER );
    final RowConstraints r = new RowConstraints();
    r.setPercentHeight( 100 );

    switch ( layout )
    {
      case BOTTOM:
        top.setPercentHeight( 20 );
        bottom.setPercentHeight( 80 );

        monopolySquare.getRowConstraints().addAll( top, bottom );
        monopolySquare.getColumnConstraints().addAll( c );

        monopolySquare.add( colorPane, 0, 0 );
        monopolySquare.add( name, 0, 1 );
        break;
      case LEFT:
        right.setPercentWidth( 20 );
        left.setPercentWidth( 80 );

        monopolySquare.getColumnConstraints().addAll( left, right );
        monopolySquare.getRowConstraints().addAll( r );

        monopolySquare.add( name, 0, 0 );
        monopolySquare.add( colorPane, 1, 0 );
        break;
      case RIGHT:
        right.setPercentWidth( 80 );
        left.setPercentWidth( 20 );

        monopolySquare.getColumnConstraints().addAll( left, right );
        monopolySquare.getRowConstraints().addAll( r );

        monopolySquare.add( colorPane, 0, 0 );
        monopolySquare.add( name, 1, 0 );
        break;
      case TOP:
        top.setPercentHeight( 80 );
        bottom.setPercentHeight( 20 );

        monopolySquare.getRowConstraints().addAll( top, bottom );
        monopolySquare.getColumnConstraints().addAll( c );
        bottom.setValignment( VPos.CENTER );

        monopolySquare.add( colorPane, 0, 1 );
        monopolySquare.add( name, 0, 0 );
        break;
    }

然而,这不会旋转标签(我认为无论如何对于数字垄断板来说更好;))。一旦旋转标签,就会遇到与单元格相同的问题,只有更小的问题。

完整code example