添加的面板未显示在框架中(Java) [英] Added panel not showing up in frame (Java)

查看:64
本文介绍了添加的面板未显示在框架中(Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java中的swing来制作战舰游戏板.我已经将面板(即网格或游戏板)添加到了窗口中,但它仍然不会显示.我粘贴了下面的代码.任何帮助表示赞赏.谢谢.

I'm working on making a battleship game board using swing in java. I've added the panel (i.e. the grid or the game board) to the window but it still won't show up. I've pasted the code below. Any help is appreciated. Thanks.

public class board {

private static JFrame window;
private static JPanel[][] grid;

public static void main(String[] args) {
    window = new JFrame();
    window.setTitle("GAME BOARD");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    grid = new JPanel[10][10];
    for (int i =0; i< 10; i++) {
        for (int j =0; j< 10; j++) {
            grid[i][j] = new JPanel();
            grid[i][j].setBackground(Color.white);
            grid[i][j].setBorder(BorderFactory.createLineBorder(Color.black));
            grid[i][j].setPreferredSize(new Dimension(25,25));
            window.add(grid[i][j]);
        }
    }
    window.pack();
    window.setVisible(true);
}

}

推荐答案

默认情况下,调用JFrame#add()将添加到JFrame contentPane 中,其默认布局为

By default calling JFrame#add() will add to the JFrames contentPane which has a default layout of BorderLayout.

请阅读布局管理器可视指南.您可能要使用 GridLayout FlowLayout ,具体取决于您的需求.

Have a read on A Visual Guide to Layout Managers. You probably want to either use a GridLayout or FlowLayout depending on your needs.

我还建议覆盖getPreferredSize并返回尺寸,而不是调用setPreferredSize.

I'd also suggest overriding getPreferredSize and returning the dimensions as opposed to calling setPreferredSize.

然后您将执行以下操作:

You then would do something like this:

// this jpanel will hold the all grid jpanels could also use new FlowLayout()
JPanel container = new JPanel(new GridLayout(10,10));
grid = new JPanel[10][10];
    for (int i =0; i< 10; i++) {
        for (int j =0; j< 10; j++) {
           grid[i][j] = new JPanel() {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(25, 25);
                }
            };
            ...
            container.add(grid[i][j]);
        }
    }

window.add(container);
window.pack();
window.setVisible(true);

看看这个问题,它也是关于创建战舰游戏的布局.我建议使用更轻便,更易于自定义的内容,例如JLabel来代表每个正方形.

Have a look at this question which is also about creating a layout for a battleship game. I'd suggest using something more lightweight and easier to customize like JLabel to represent each square.

您可能还想阅读事件调度线程

基本上所有 Swing 组件都应通过SwingUtilities.invokeLater(...)在EDT上创建:

Essentially all Swing components should be created on the EDT via SwingUtilities.invokeLater(...):

import javax.swing.SwingUtilities;

public class TestApp {

    public TestApp() {
        createAndShowGui();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(TestApp::new);
    }

    private void createAndShowGui() {
         // create JFrame and other Swing components here
    }
}

这篇关于添加的面板未显示在框架中(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆