面板中的GridLayout未显示按钮

时间:2016-06-20 16:46:28

标签: java swing netbeans

我创建了一个扩展名为JPanel GamePane的类,在创建时将布局设置为GridLayout,然后一次添加一个按钮:

public class GamePane extends JPanel {
    private JButton[][] buttons;
    private final int sizeX, sizeY;
    private Partida partida;

    public GamePane(int sizeX, int sizeY, Partida p) {
        super();
        this.sizeX = sizeX;
        this.sizeY = sizeY;
        this.partida = p;
        this.setLayout(new GridLayout(sizeX + 1, sizeY + 1));
        buttons = new JButton[sizeX + 1][sizeY + 1];

        for (int y = 0; y < sizeY + 1; y++) {
            for (int x = 0; x < sizeX + 1; x++) {
                JButton button = new JButton();
                button.addActionListener(new ListenerGameButton(x, y, this));
                this.add(button);
                button.setBackground(Color.yellow);
                button.setVisible(true);
                buttons[x][y] = button;
            }
        }

面板初始化在父JDialog上完成:

gamePane = new GamePane(sizeX, sizeY, parentPartida);

以下是它在NetBeans编辑器中的外观:(所选框架是GamePane的一个实例)

然后在测试时它没有显示按钮:

这是孩子GamePane所在的父JDialog的类,大部分代码由Netbeans自动生成:

public class gameDialog extends javax.swing.JDialog {

private final int sizeX, sizeY;
private Partida parentPartida;

public gameDialog(java.awt.Frame parent, boolean modal, int sizeX, int sizeY, Partida parentPartida)
{
    super(parent, modal);
    this.parentPartida = parentPartida;
    this.sizeX = sizeX;
    this.sizeY = sizeY;
    initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    logoPane = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    gamePane = new GamePane(sizeX, sizeY, parentPartida);

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("Partida en curso");
    setModal(true);
    setResizable(false);

    logoPane.setBackground(new java.awt.Color(95, 143, 191));

    jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/logo.png"))); // NOI18N
    jLabel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(0, 0, 0), new java.awt.Color(0, 0, 0), new java.awt.Color(0, 0, 0), new java.awt.Color(0, 0, 0)));
    jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

    javax.swing.GroupLayout logoPaneLayout = new javax.swing.GroupLayout(logoPane);
    logoPane.setLayout(logoPaneLayout);
    logoPaneLayout.setHorizontalGroup(
        logoPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    logoPaneLayout.setVerticalGroup(
        logoPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout gamePaneLayout = new javax.swing.GroupLayout(gamePane);
    gamePane.setLayout(gamePaneLayout);
    gamePaneLayout.setHorizontalGroup(
        gamePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 437, Short.MAX_VALUE)
    );
    gamePaneLayout.setVerticalGroup(
        gamePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 353, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(gamePane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(logoPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(logoPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGap(18, 18, 18)
            .addComponent(gamePane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap())
    );

    pack();
}// </editor-fold>                        

// Variables declaration - do not modify                     
private GUI.GamePane gamePane;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel logoPane;
// End of variables declaration                   

}

initComponents()方法由NetBeans生成,这是创建GamePane的地方,稍后通过调用pack()在initComponents()的末尾打包框架 那么,我做错了什么?提前谢谢!

1 个答案:

答案 0 :(得分:2)

您的代码没有问题。它完美地工作。

JDialog dialog = new JDialog();
dialog.getContentPane().add(new GamePane(10, 10));
dialog.pack();
dialog.setVisible(true);

我得到了以下内容。

enter image description here