我如何摆脱Jframe底部的这条灰线

发布于 2025-01-23 16:22:07 字数 1188 浏览 2 评论 0原文

我正在尝试使用多个jpanels创建一个40x40网格,然后在设法创建它之后,jframe的底部有一个空的空间。

public drawBoard() {
    
    createMap(40, 40);
}

public void createMap(int maxX, int maxY) {
    String [ ][ ] map = new String [maxX][maxY];

    //create 40 panel for the snake to move around in
    for (int i=1; i < maxX; i++) {
        for (int j=1; j < maxY; j++) {

            JPanel panel = new JPanel();
            add(panel);
            panel.setPreferredSize(pref_size);
            String name = String.format("[%d, %d]", i, j);
            setLayout(new GridLayout(maxX, maxY, 1, 1)); //change gap to 0 after tests are done
            setBackground(Color.GRAY); 
            panel.setName(name);

        }
    }
}

private static void createFrame() {
    JFrame frame = new JFrame("Snake");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new drawBoard());
    frame.pack();
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
    frame.setResizable(false);
};

I am trying to create a 40x40 grid using multiple JPanels and after I managed to create it, there is this empty space at the bottom of the JFrame.

public drawBoard() {
    
    createMap(40, 40);
}

public void createMap(int maxX, int maxY) {
    String [ ][ ] map = new String [maxX][maxY];

    //create 40 panel for the snake to move around in
    for (int i=1; i < maxX; i++) {
        for (int j=1; j < maxY; j++) {

            JPanel panel = new JPanel();
            add(panel);
            panel.setPreferredSize(pref_size);
            String name = String.format("[%d, %d]", i, j);
            setLayout(new GridLayout(maxX, maxY, 1, 1)); //change gap to 0 after tests are done
            setBackground(Color.GRAY); 
            panel.setName(name);

        }
    }
}

private static void createFrame() {
    JFrame frame = new JFrame("Snake");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new drawBoard());
    frame.pack();
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
    frame.setResizable(false);
};

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤还要旧人安 2025-01-30 16:22:08

找到一个答案,看起来是39x39网格而不是40x40,一旦更改了网格,就没有空白。都很好

Found an answer, looks like it was a 39x39 grid instead of 40x40, once changed the grid just fell into place with no blank spaces.all good

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文