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沒有。我也將增加一個圖片向你展示我的問題是什麼:
有誰知道如何解決這個問題?我知道我可以將它放在一個組中,但將它放入組中的問題是組不會調整到GridPane的單元格。
感謝您的回覆! –