javafx 调整组件及其位置

javafx resize the components and their positions

我正在寻找如何调整列(红色和白色)的大小并在调整 window 大小时保持它们在板上的位置。

正确

问题

板上的代码架构和列绑定。

//the points of columns in the board map
private int[][] whitepoints={{54,27},{235,27},{417,27}};
private int[][] redpoints={{145,27},{325,27},{507,27}};

StartBoard(Stage primaryStage){

    board.fitWidthProperty().bind(primaryStage.widthProperty());
    board.fitHeightProperty().bind(primaryStage.heightProperty());
    
    for(int i=0; i<3; i++){

        whiteArray[i]= new ImageView(white);
        whiteArray[i].setLayoutX(whitepoints[i][0]);
        whiteArray[i].setLayoutY(whitepoints[i][1]);
        
        redArray[i]= new ImageView(red);
        redArray[i].setLayoutX(redpoints[i][0]);
        redArray[i].setLayoutY(redpoints[i][1]);
            
    }

}

启动方法代码

Pane root = new Pane();
 
Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();

Scene scene = new
Scene(root,screenBounds.getWidth(),screenBounds.getHeight());

primaryStage.setTitle("Backgammon");
primaryStage.setScene(scene);
//primaryStage.setMaximized(true);
//primaryStage.setResizable(false);
primaryStage.show();

注释是防止用户调整大小的替代方法 window。

任何关于其他技术的建议都是可取的,因为我知道这不是正确的方法。

您可以使用 GridPane 来定位三角形。

private static Node createTriangle(boolean up, boolean red) {
    final double height = 100d;
    final double width = 40d;
    Polygon result = up ? new Polygon(
            0d, height,
            width / 2, 0d,
            width, height
    ) : new Polygon(
            0d, 0d,
            width / 2, height,
            width, 0d);

    result.setFill(red ? Color.RED : Color.WHITE);
    return result;
}

private static GridPane generateBoard() {
    GridPane board = new GridPane();

    ColumnConstraints columnConstraints = new ColumnConstraints();
    columnConstraints.setHgrow(Priority.ALWAYS);
    columnConstraints.setHalignment(HPos.CENTER);

    RowConstraints rowConstraintsTop = new RowConstraints();
    rowConstraintsTop.setVgrow(Priority.ALWAYS);
    rowConstraintsTop.setValignment(VPos.TOP);

    RowConstraints rowConstraintsBottom = new RowConstraints();
    rowConstraintsBottom.setVgrow(Priority.ALWAYS);
    rowConstraintsBottom.setValignment(VPos.BOTTOM);

    board.getColumnConstraints().addAll(Stream.generate(() -> columnConstraints).limit(6).toArray(ColumnConstraints[]::new));
    board.getRowConstraints().addAll(rowConstraintsTop, rowConstraintsBottom);

    board.addRow(0, IntStream.range(0, 6).mapToObj(i -> createTriangle(false, (i & 1) != 0)).toArray(Node[]::new));
    board.addRow(1, IntStream.range(0, 6).mapToObj(i -> createTriangle(true, (i & 1) == 0)).toArray(Node[]::new));

    board.setStyle("-fx-background-color: #444;");
    return board;
}

@Override
public void start(Stage primaryStage) {
    GridPane board1 = generateBoard(), board2 = generateBoard();
    HBox.setHgrow(board1, Priority.ALWAYS);
    HBox.setHgrow(board2, Priority.ALWAYS);
    HBox root = new HBox(20, board1, board2);
    root.setFillHeight(true);
    root.setPadding(new Insets(20));
    root.setStyle("-fx-background-color: brown;");

    Scene scene = new Scene(root, 800, 300);
    primaryStage.setScene(scene);
    primaryStage.show();
}