如何将边框添加到GridPane的一部分?

时间:2017-07-09 06:56:19

标签: css javafx gridpane

我试图接近"分组"网格窗格(或框架)中的单元格

所以这基本上是我的网格窗格:

GridPane grid = new GridPane();
grid.setHgap(H_SIZE);
grid.setVgap(V_SIZE);

我想做这样的事情:

grid.setBorder(1,1, H_SIZE/3, V_SIZE/3);
grid.setBorder(H_SIZE*2/3,V_SIZE*2/3, H_SIZE-1, V_SIZE-1);

我还希望能够更改边框,以防程序运行到窗格时发生更改(即向窗格添加新按钮)。

提前致谢。

所以通过一些工作,我设法按照我的意愿去做:

public void setBorder(GridPane grid, int rowStart, int colStart, int rowEnd, int colEnd){
    Node node;
    node = getNodeByRowColumnIndex(rowStart, colStart, grid);
    node.getStyleClass().add("game-grid-cell");
    node.getStyleClass().add("corner-top-left");
    node = getNodeByRowColumnIndex(rowEnd, colStart, grid);
    node.getStyleClass().add("game-grid-cell");
    node.getStyleClass().add("corner-bot-left");
    node = getNodeByRowColumnIndex(rowEnd, colEnd, grid);
    node.getStyleClass().add("game-grid-cell");
    node.getStyleClass().add("corner-bot-right");
    node = getNodeByRowColumnIndex(rowStart, colEnd, grid);
    node.getStyleClass().add("game-grid-cell");
    node.getStyleClass().add("corner-top-right");

    for(int i = rowStart + 1; i < rowEnd; i++){
        node = getNodeByRowColumnIndex(i, colStart, grid);
        node.getStyleClass().add("game-grid-cell");
        node.getStyleClass().add("left");

        node = getNodeByRowColumnIndex(i, colEnd, grid);
        node.getStyleClass().add("game-grid-cell");
        node.getStyleClass().add("right");
    }

    for(int i = colStart+1; i < colEnd; i++){
        node = getNodeByRowColumnIndex(rowStart, i, grid);
        node.getStyleClass().add("game-grid-cell");
        node.getStyleClass().add("top");

        node = getNodeByRowColumnIndex(rowEnd, i, grid);
        node.getStyleClass().add("game-grid-cell");
        node.getStyleClass().add("bot");
    }
}

public Node getNodeByRowColumnIndex (final int row, final int column, GridPane gridPane) {
    Node result = null;
    ObservableList<Node> childrens = gridPane.getChildren();

    for (Node node : childrens) {
        if(GridPane.getRowIndex(node) == row && GridPane.getColumnIndex(node) == column) {
            return node;
        }
    }

    return result;
}

CSS文件是game.css:

.game-grid {
    -fx-background-color: white ;
    -fx-padding: 10 ;
}
.game-grid-cell {
    -fx-background-color: blue, white ;
    /*-fx-background-insets: 0, 0 1 1 0 ;*/
}
.game-grid-cell.corner-top-left{
    -fx-background-radius: 10 0 0 0;
    -fx-background-insets: 0, 1 0 0 1 ;
}
.game-grid-cell.corner-top-right{
    -fx-background-radius: 0 10 0 0;
    -fx-background-insets: 0, 1 1 0 0 ;
}
.game-grid-cell.corner-bot-left{
    -fx-background-radius: 0 0 0 10;
    -fx-background-insets: 0, 0 0 1 1 ;
}
.game-grid-cell.corner-bot-right{
    -fx-background-radius: 0 0 10 0;
    -fx-background-insets: 0, 0 1 1 0 ;
}
.game-grid-cell.top{
    -fx-background-insets: 0, 1 0 0 0 ;
}
.game-grid-cell.bot{
    -fx-background-insets: 0, 0 0 1 0 ;
}
.game-grid-cell.right{
    -fx-background-insets: 0, 0 1 0 0 ;
}
.game-grid-cell.left{
    -fx-background-insets: 0, 0 0 0 1 ;
}

0 个答案:

没有答案