适合GridPane JavaFx

时间:2017-03-30 23:49:48

标签: javafx pane

我一直在尝试选择最适合我的元素的Pane。我试过FlowPane和GridPane,但我得到了相同的结果。

Screenshot with the problem 我想要的结果是将第1列第1行的playerCards网格放在第1列第0行的playerInfo网格下。这不会发生,因为第0列第0行的元素高度的高度是大。并且下一行中的所有元素都根据此定位。所以基本上我的目标是删除红色背景和白色背景元素之间的空间。

private Node getTop() {
    GridPane container = new GridPane();
    FlowPane topp = new FlowPane();
    topp.setMinHeight(200);
    GridPane playerInfo = new GridPane();
    playerInfo.setHgap(125.0);
    playerInfo.setVgap(5.0);
    playerInfo.setPadding(new Insets(10.0, 10.0, 0.0, 50.0));

    //most left here
    GridPane legend = new GridPane();
    legend.setStyle("-fx-background-color: #FFFFFF;");
    legend.setVgap(2.0);
    Label[] treasureTypes = new Label[7];
    treasureTypes[0] = new Label(" - Diamonds           5pts");
    treasureTypes[1] = new Label(" - Rubies                5pts");
    treasureTypes[3] = new Label(" - Pearls                 3pts");
    treasureTypes[2] = new Label(" - Gold Bars           4pts");
    treasureTypes[4] = new Label(" - Barrels of Rum   2pts");
    treasureTypes[5] = new Label(" - Black card");
    treasureTypes[6] = new Label(" - Red card");

    for (int x = 0; x < treasureTypes.length; ++x) {
        switch (x) {
            case 0:
                legend.add(new ImageView(new Image("group/project/images/diamond.png")), 0, x);
                break;
            case 1:
                legend.add(new ImageView(new Image("group/project/images/diamond.png")), 0, x);
                break;
            case 2:
                legend.add(new ImageView(new Image("group/project/images/pearls.jpg")), 0, x);
                break;
            case 3:
                legend.add(new ImageView(new Image("group/project/images/gold_bar.png")), 0, x);
                break;
            case 4:
                legend.add(new ImageView(new Image("group/project/images/diamond.png")), 0, x);
                break;
            case 5:
                legend.add(new ImageView(new Image("group/project/images/diamond.png")), 0, x);
                break;
            case 6:
                legend.add(new ImageView(new Image("group/project/images/diamond.png")), 0, x);
                break;
        }
        legend.add(treasureTypes[x], 1, x);
    }
    legend.setPadding(new Insets(10.0, 10.0, 0.0, 50.0));

    //top.getChildren().add(left);
    //right buttons here
    Button rules = new Button("Rules");
    Button idk = new Button("Chance Card References");
    VBox rightButtons = new VBox();
    rightButtons.setAlignment(Pos.CENTER);
    rightButtons.setMinWidth(200.0);
    rightButtons.getChildren().addAll(rules, idk);

    Label playerNames[] = new Label[4];
    Label playerCoords[] = new Label[4];
    Label playerMoveRange[] = new Label[4];
    Label playerHomeTown[] = new Label[4];

    Player pl;
    for (int x = 0; x < 4; ++x) {

        pl = gameEngine.getPlayer(x);

        playerNames[x] = new Label();
        playerCoords[x] = new Label();
        playerMoveRange[x] = new Label();
        playerHomeTown[x] = new Label();

        //highlight the current player nickname
        if (pl.getNickname().equals(gameEngine.getCurrentPlayer().getNickname())) {
            playerNames[x].setTextFill(Color.GREEN);
        } else {
            //this is for reseting the color i.e the color is currently green
            playerNames[x].setTextFill(Color.BLACK);
        }
        playerNames[x].setText("Player :" + pl.getNickname());
        playerCoords[x].setText("Coordinates : ("
                + Integer.toString(pl.getShip().getCoordinates().getColumn() + 1)
                + ", " + Integer.toString(pl.getShip().getCoordinates().getRow() + 1) + ")");

        playerMoveRange[x].setText("Player move range: " + pl.getMovementRange());
        playerHomeTown[x].setText("Home town: " + pl.getShip().getHomeTown().getName());

        playerInfo.add(playerNames[x], x, 0);
        playerInfo.add(playerCoords[x], x, 1);
        playerInfo.add(playerMoveRange[x], x, 2);
        playerInfo.add(playerHomeTown[x], x, 3);
    }
    GridPane playerCards = new GridPane();
    int numCards = 0;
    int cardColor, cardVal;

    for (Card c : gameEngine.getCurrentPlayer().getCards()) {
        cardColor = (c.getColor()) ? 1 : 0;
        cardVal = c.getValue();
        Image cardImg = null;

        switch (cardColor) {
            case 0:
                switch (cardVal) {
                    case 1:
                        cardImg = new Image("group/project/images/diamond.png");
                        break;
                    case 2:
                        cardImg = new Image("group/project/images/diamond.png");
                        break;
                    case 3:
                        cardImg = new Image("group/project/images/diamond.png");
                        break;
                }
                break;
            case 1:
                switch (cardVal) {
                    case 1:
                        cardImg = new Image("group/project/images/diamond.png");
                        break;
                    case 2:
                        cardImg = new Image("group/project/images/diamond.png");
                        break;
                    case 3:
                        cardImg = new Image("group/project/images/diamond.png");
                        break;
                }
                break;
        }
        playerCards.add(new ImageView(cardImg), numCards, 0);
        ++numCards;
    }
    playerCards.setPadding(new Insets(0.0, 10.0, 0.0, 75.0));

    playerCards.setStyle("-fx-background-color: #FFFFFF;");
    playerInfo.setStyle("-fx-background-color: red;");
    playerInfo.setMaxHeight(100);
    container.add(legend, 0, 0);
    container.add(playerInfo, 1, 0);
    container.add(rightButtons, 2, 0);
    container.add(playerCards, 1, 1);

    return container;
}

这是返回Node - GridPane的整个函数,然后设置为borderPane的顶部。即borderPane.setTop(getTop());

0 个答案:

没有答案