Java 如何在不使用 setGridLinesVisible() 方法的情况下永久显示 GridPane 对象网格线?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37619867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display GridPane object grid lines permanently, and without using the setGridLinesVisible() method?
提问by RoastedCode
Is it possible to make all GridPane's grid lines permanently visible without using setGridLinesVisible()
?
是否可以在不使用的情况下使所有 GridPane 的网格线永久可见setGridLinesVisible()
?
I know that setGridLinesVisible()
is for debugging purposes only, and I want to show grid lines for the end-user's convenience.
我知道这setGridLinesVisible()
仅用于调试目的,为了方便最终用户,我想显示网格线。
Also, I need to work on a Pane container and not a Canvas.
另外,我需要处理 Pane 容器而不是 Canvas。
My program is able to add, delete, modify, move, etc. shape objects and groups on a Pane type or a Pane subtype parent container.
我的程序能够在窗格类型或窗格子类型父容器上添加、删除、修改、移动等形状对象和组。
Thanks
谢谢
采纳答案by James_D
Doing this depends a bit on how you have things set up. From the description of your application, when I've experimented with similar things I always found it convenient to fill the grid with empty Pane
s of some kind to act as the cells, and then to manipulate their content based on the data in the model. If you use this approach, you can use some CSS to put borders (e.g. using nested backgrounds) on the "cells", which will give the effect of grid lines.
这样做在一定程度上取决于您的设置方式。从您的应用程序的描述来看,当我尝试类似的事情时,我总是发现用Pane
某种空的s填充网格作为单元格,然后根据模型中的数据操作它们的内容很方便。如果您使用这种方法,您可以使用一些 CSS 在“单元格”上放置边框(例如使用嵌套背景),这将产生网格线的效果。
Here's a simple example of this approach:
这是这种方法的一个简单示例:
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);
}
}
and the CSS (grid-with-borders.css
):
和 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 ;
}
回答by Sedrick
This worked for me:
这对我有用:
GridPane grid = new GridPane();
grid.setGridLinesVisible(true);
for debug only!
仅供调试!
回答by Jroosterman
If you are trying to use fxml you can try this:
如果你想使用 fxml 你可以试试这个:
<GridPane GridPane.columnIndex="1" GridPane.rowIndex="0" gridLinesVisible="true">
回答by nullmn
For every children use:
对于每个孩子使用:
-fx-border-color: black;
-fx-border-width: 0 0 1 1;
This can be achieved with the yourChildNode.setStyle(css)
method for example.
例如,这可以通过该yourChildNode.setStyle(css)
方法来实现。
Should one cell contain multiple layouts wrap them in one AnchorPane
and apply the CSS on the AnchorPane
.
如果一个单元格包含多个布局,请将它们AnchorPane
合二为一并在AnchorPane
.