2016-02-27 96 views
1

我在使用JavaFX中的setRotate()旋轉它之後,節點(GridPane)的方向出現問題。當我旋轉節點並將其放入GridPane的一個單元格中時,我希望旋轉的節點適合單元格內部,並調整單元格大小。我添加了一些示例代碼來向您展示我想要的結果。JavaFX將旋轉對象調整爲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

那麼,這是一個艱難的。想不到最佳的解決方案,但如果我處於你的位置,我會盡量保持在佈局的限制內(不要使用像旋轉這樣的低級操作)。

例如,建立一個不同的佈局單元的每個行/列:

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

+0

感謝您的回覆! –