无法将JButtons数组添加到JPanel

时间:2013-07-27 03:34:04

标签: java arrays swing jpanel jbutton

public game()
{
    setTitle("game");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setResizable(false);
    setLayout(new BorderLayout());
    buildMenu();
    setJMenuBar(menuBar);
    buildGreetingsPanel();
    add(greetingsPanel, BorderLayout.NORTH);
    buildGamePanel();
    add(gamePanel, BorderLayout.CENTER);
    buildStatusPanel();
    add(statusPanel, BorderLayout.SOUTH);
    buildSettingsPanel();
    add(settingsPanel);
    pack();
    setVisible(true);
}

[...............]

private void buildGamePanel()
{
    gamePanel = new JPanel();
    gamePanel.setBorder(BorderFactory.createLineBorder(difficultyColor));
    gamePanel.setPreferredSize(new Dimension(9 * span, 9 * span));
    gamePanel.setLayout(new GridLayout(40, 40, 0, 0));

    for(col = 0; col < gameWidth; col++)
    {
        for(row = 0; row < gameHeight; row++)
        {
            buttons[col][row] = new JButton();
            buttons[col][row].setBounds(4, 4, span, span);
            buttons[col][row].addMouseListener(new mouseListener());
            gamePanel.add(buttons[col][row]);
        }
    }
}

正如标题所示 我添加了buildGamePanel();add(gamePanel, BorderLayout.CENTER); 已经。有什么问题?感谢

编辑:由于没有人回答,问题可能在其他地方。 我添加了公共游戏();构造函数可能是错误的来源。

1 个答案:

答案 0 :(得分:0)

问题在于BorderLayout();

编译时,在gamePanel之前将settingsPanel添加到GUI的中心。 忘了指定它应该放置如下:

    add(settingsPanel, BorderLayout.EAST);