如何永久显示GridPane对象网格线,而不使用setGridLinesVisible()方法?

Roa*_*ode 6 java user-interface javafx pane gridpane

是否可以在不使用的情况下使所有GridPane的网格线永久可见setGridLinesVisible()

我知道这setGridLinesVisible()仅用于调试目的,我想显示网格线以方便最终用户.

另外,我需要处理Pane容器而不是Canvas.

我的程序能够在Pane类型或Pane子类型父容器上添加,删除,修改,移动等形状对象和组.

谢谢

Jam*_*s_D 9

这样做取决于你如何设置东西.从你的应用程序的描述,当我尝试类似的东西时,我总是发现用Pane一些空的s 填充网格作为单元格,然后根据模型中的数据操纵它们的内容很方便.如果使用这种方法,可以使用一些CSS在"单元格"上放置边框(例如使用嵌套背景),这将产生网格线的效果.

以下是此方法的一个简单示例:

import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.scene.Scene;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class GridPaneWithLines extends Application {


    private StackPane createCell(BooleanProperty cellSwitch) {

        StackPane cell = new StackPane();

        cell.setOnMouseClicked(e -> cellSwitch.set(! cellSwitch.get() ));

        Circle circle = new Circle(10, Color.CORNFLOWERBLUE);

        circle.visibleProperty().bind(cellSwitch);

        cell.getChildren().add(circle);
        cell.getStyleClass().add("cell");
        return cell;
    }

    private GridPane createGrid(BooleanProperty[][] switches) {

        int numCols = switches.length ;
        int numRows = switches[0].length ;

        GridPane grid = new GridPane();

        for (int x = 0 ; x < numCols ; x++) {
            ColumnConstraints cc = new ColumnConstraints();
            cc.setFillWidth(true);
            cc.setHgrow(Priority.ALWAYS);
            grid.getColumnConstraints().add(cc);
        }

        for (int y = 0 ; y < numRows ; y++) {
            RowConstraints rc = new RowConstraints();
            rc.setFillHeight(true);
            rc.setVgrow(Priority.ALWAYS);
            grid.getRowConstraints().add(rc);
        }

        for (int x = 0 ; x < numCols ; x++) {
            for (int y = 0 ; y < numRows ; y++) {
                grid.add(createCell(switches[x][y]), x, y);
            }
        }

        grid.getStyleClass().add("grid");
        return grid;
    }

    @Override
    public void start(Stage primaryStage) {
        int numCols = 5 ;
        int numRows = 5 ;

        BooleanProperty[][] switches = new BooleanProperty[numCols][numRows];
        for (int x = 0 ; x < numCols ; x++) {
            for (int y = 0 ; y < numRows ; y++) {
                switches[x][y] = new SimpleBooleanProperty();
            }
        }

        GridPane grid = createGrid(switches);

        StackPane root = new StackPane(grid);
        Scene scene = new Scene(root, 600, 600);
        scene.getStylesheets().add("grid-with-borders.css");
        primaryStage.setScene(scene);
        primaryStage.show();
    }




    public static void main(String[] args) {
        launch(args);
    }
}
Run Code Online (Sandbox Code Playgroud)

和CSS(grid-with-borders.css):

.root {
    -fx-padding: 20 ;
    cell-color: white ;
    cell-border-color: black ;
}
.grid {
    /* 1 pixel border around the top and right of the grid: */
    -fx-background-color: cell-border-color, cell-color ;
    -fx-background-insets: 0, 1 1 0 0 ;
    -fx-padding: 1 ;
}
.cell {
    /* 1 pixel border around the left and bottom of each cell: */
    -fx-background-color: cell-border-color, cell-color ;
    -fx-background-insets: 0, 0 0 1 1 ;
}
Run Code Online (Sandbox Code Playgroud)


Sed*_*ick 5

这对我有用:

GridPane grid = new GridPane();
grid.setGridLinesVisible(true);
Run Code Online (Sandbox Code Playgroud)

仅用于调试!


Jro*_*man 5

如果您尝试使用 fxml,您可以尝试以下操作:

    <GridPane GridPane.columnIndex="1"  GridPane.rowIndex="0" gridLinesVisible="true">
Run Code Online (Sandbox Code Playgroud)