向 GridPane JavaFX 添加边框

新手上路,请多包涵

我正在使用 GridPane 在 JavaFX 中创建棋盘游戏。

有 7 种不同的动画可以放置在网格的每个网格(单元格)中。

最初网格看起来像这样

空网格

在编写动画插入程序之前,我测试了向其中添加一个简单的圆圈。它看起来像这样

带有子场景的网格窗格

在此处输入图像描述

添加的节点是包含 TimeLine 动画的 SubScenes。每个单元格大小为 40x40,SubScene 大小也是 40x40。

添加后的子场景位于网格窗格边框线的顶部,看起来不太好。

我该怎么做才能将节点添加到网格线下方?即网格线位于节点之上。

如果 GridPane 不可能,还有什么我可以使用的吗?

我为游戏执行的课程

class Game {
    static GridPane grid;
    public void start(final Stage stage) throws Exception {
        int rows = 5;
        int columns = 5;

        stage.setTitle("Enjoy your game");
        grid = new GridPane();
        for(int i = 0; i < columns; i++) {
            ColumnConstraints column = new ColumnConstraints(40);
            grid.getColumnConstraints().add(column);
        }

        for(int i = 0; i < rows; i++) {
            RowConstraints row = new RowConstraints(40);
            grid.getRowConstraints().add(row);
        }

        grid.setOnMouseReleased(new EventHandler<MouseEvent> () {
            public void handle(MouseEvent me) {
                grid.add(Anims.getAnim(1), (int)((me.getSceneX() - (me.getSceneX() % 40)) / 40), (int)((me.getSceneY() - (me.getSceneY() % 40)) / 40)); //here the getAnim argument could be between 1-7
            }
        });

        grid.setStyle("-fx-background-color: white; -fx-grid-lines-visible: true");
        Scene scene = new Scene(grid, (columns * 40) + 100, (rows * 40) + 100, Color.WHITE);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(final String[] arguments) {
        Application.launch(arguments);
    }
}

包含动画的类,这里我只是创建一个圆圈

public class Anims {

    public static SubScene getAnim(final int number) throws Exception {
        Circle circle = new Circle(20, 20f, 7);
        circle.setFill(Color.RED);
        Group group = new Group();
        group.getChildren().add(circle);
        SubScene scene = new SubScene(group, 40, 40);
        scene.setFill(Color.WHITE);
        return scene;
    }
}

原文由 Jeet Parekh 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.3k
2 个回答

不要使用 setGridLinesVisible(true)文档 明确指出这仅用于调试。

相反,在所有网格单元格(甚至是空单元格)中放置一个窗格,并设置窗格样式以便您看到边框。 (这让您有机会非常仔细地控制边框,这样您就可以避免出现双边框等情况。)然后将内容添加到每个窗格中。您还可以向窗格注册鼠标侦听器,这意味着您不必进行难看的数学运算来确定单击了哪个单元格。

将边框应用于任何区域的推荐方法是使用 CSS 和“嵌套背景”方法。在这种方法中,您在区域上绘制两个(或更多)背景填充,具有不同的插图,呈现出边框的外观。例如:

 -fx-background-fill: black, white ;
-fx-background-insets: 0, 1 ;

将首先绘制一个没有插图的黑色背景,然后在其上方绘制一个白色背景,在所有边上都有 1 个像素的插图,给出一个宽度为 1 像素的黑色边框的外观。虽然这似乎违反直觉,但它的性能(据称)比直接指定边界要好。您还可以为 每个 填充的插图指定一个包含四个值的序列,这些值分别被解释为顶部、右侧、底部和左侧的插图。所以

-fx-background-fill: black, white ;
-fx-background-insets: 0, 0 1 1 0 ;

在右侧和底部等处具有黑色边框的效果。

我也不确定 SubScene 是你真正想要的,除非你打算在每个单元上安装不同的相机。如果你真的需要一个子场景,让填充透明以避免绘制在单元格的边缘。您可以将 Group 直接添加到每个单元格(您可能只添加圆圈,具体取决于您的需要…)。

就像是:

 import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.RowConstraints;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class Game2 extends Application{
    @Override
    public void start(final Stage stage) throws Exception {
        int rows = 5;
        int columns = 5;

        stage.setTitle("Enjoy your game");

        GridPane grid = new GridPane();
        grid.getStyleClass().add("game-grid");

        for(int i = 0; i < columns; i++) {
            ColumnConstraints column = new ColumnConstraints(40);
            grid.getColumnConstraints().add(column);
        }

        for(int i = 0; i < rows; i++) {
            RowConstraints row = new RowConstraints(40);
            grid.getRowConstraints().add(row);
        }

        for (int i = 0; i < columns; i++) {
            for (int j = 0; j < rows; j++) {
                Pane pane = new Pane();
                pane.setOnMouseReleased(e -> {
                    pane.getChildren().add(Anims.getAtoms(1));
                });
                pane.getStyleClass().add("game-grid-cell");
                if (i == 0) {
                    pane.getStyleClass().add("first-column");
                }
                if (j == 0) {
                    pane.getStyleClass().add("first-row");
                }
                grid.add(pane, i, j);
            }
        }

        Scene scene = new Scene(grid, (columns * 40) + 100, (rows * 40) + 100, Color.WHITE);
        scene.getStylesheets().add("game.css");
        stage.setScene(scene);
        stage.show();
    }

    public static class Anims {

        public static Node getAtoms(final int number) {
            Circle circle = new Circle(20, 20f, 7);
            circle.setFill(Color.RED);
            Group group = new Group();
            group.getChildren().add(circle);
//            SubScene scene = new SubScene(group, 40, 40);
//            scene.setFill(Color.TRANSPARENT);
            return group;
        }
    }

    public static void main(final String[] arguments) {
        Application.launch(arguments);
    }
}

和CSS:

 .game-grid {
    -fx-background-color: white ;
    -fx-padding: 10 ;
}
.game-grid-cell {
    -fx-background-color: black, white ;
    -fx-background-insets: 0, 0 1 1 0 ;
}
.game-grid-cell.first-row {
    -fx-background-insets: 0, 1 1 1 0 ;
}
.game-grid-cell.first-column {
    -fx-background-insets: 0, 0 1 1 1 ;
}
.game-grid-cell.first-row.first-column {
    -fx-background-insets: 0, 1 ;
}

原文由 James_D 发布,翻译遵循 CC BY-SA 3.0 许可协议

只需添加一个像素宽度的 H 和 V 间隙,让网格窗格的背景颜色“闪耀”:

 .my-grid-pane {
    -fx-background-color: lightgray;
    -fx-vgap: 1;
    -fx-hgap: 1;
    -fx-padding: 1;
}

如果网格窗格的背景颜色从外部扩散超过一个像素(如果其父项大于自身,则会发生),只需将网格包裹在 Group 中!

原文由 Mordechai 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题