【问题标题】:I have a 2D array of JButtons, but it seems only one of the JButton is getting added to my panel我有一个二维 JButton 数组,但似乎只有一个 JButton 被添加到我的面板中
【发布时间】:2013-12-14 00:57:21
【问题描述】:

我试图让我的 JPanel 在 10 x 10 的网格中拥有 100 个 Jbutton,但是当我运行我的代码时,只出现了 1 个大按钮。我在下面发布了我的代码,所有导入已被删除以节省空间所有必要的导入都在项目中,但此处未显示。

这是我的“SnowballFight”课程:

package snowballfight;
public class SnowballFight extends JFrame implements ActionListener{
    private JTextField display = new JTextField("Welcome to the SnowBall fight!");
    /**
     * @param args the command line arguments
     */
    public SnowballFight(){
        setLayout(new BorderLayout(1,2));
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout( 10, 10));
        display.setHorizontalAlignment(JButton.CENTER);
        display.setEditable(false);
        add(display, BorderLayout.NORTH);
        add(panel, BorderLayout.SOUTH);
    }
    public static void main(String[] args) {
        SnowballFight frame = new SnowballFight();
        GameBoard game = new GameBoard(frame);
    }
    public void actionPerformed(ActionEvent event) {
    }
}

还有我的“GameBoard”课:

package snowballfight;
public class GameBoard extends JFrame implements ActionListener{
    private JButton[][] game = new JButton[10][10];
    private JTextField display = new JTextField("Welcome to the SnowBall fight!");
    private Opponent[] opponents = new Opponent[4];


    public GameBoard(SnowballFight frame){
        for (int row = 0; row < game.length; row++) {
            for (int col = 0; col < game[row].length; col++) {
                game[row][col] = new JButton();
                game[row][col].setBackground(Color.gray);
                game[row][col].addActionListener(this);
                frame.add(game[row][col]);
            }
        }
        frame.setTitle("Snowball Fight");
        frame.setSize(200, 150);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    public boolean isGameOver(){
        for (int opponent = 0; opponent < opponents.length; opponent++) {
            if(opponents[opponent].isSoaked() == false ){
                return false;
            }
        }
        return true;
    }
    public void actionPerformed(ActionEvent event) {
    }
}

新的 GameBoard 类:

public GameBoard(SnowballFight frame){
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout( 10, 10));
        for (int row = 0; row < game.length; row++) {
            for (int col = 0; col < game[row].length; col++) {
                game[row][col] = new JButton();
                game[row][col].setBackground(Color.gray);
                game[row][col].addActionListener(this);
                panel.add(game[row][col]);
            }
        }
        add(display, BorderLayout.NORTH);
        add(panel, BorderLayout.SOUTH);
        frame.setTitle("Snowball Fight");
        frame.setSize(200, 150);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

【问题讨论】:

标签: java swing awt jbutton layout-manager


【解决方案1】:
frame.add(game[row][col]);

您确实意识到JFrame 的默认布局是BorderLayout 对吗?并且BorderLayout 的任何一个区域中只能出现一个组件?请改用GridLayout

【讨论】:

  • 这不仅仅是默认布局,它是由 OP 明确设置的;)
  • snowballFight 类的第 27 和 28 行我创建了一个 JPanel 并向该面板添加了网格布局。我正在尝试将它们添加到该网格中,不是吗?
  • 两种说法都是正确的,BorderLayout 是默认的,它被重置为 BorderLayout。
  • @user2771155 BorderLayout 应用于框架,GridLayout 应用于 JPanel,该 JPanel 已添加到框架中,但是,按钮已添加到框架中......而不是移动“面板”到 GameBoard 构造函数并将您的按钮添加到其中,然后将其添加到框架中
  • 好吧,我将面板添加到我的 Gameboard 类中,而不是将按钮添加到面板中。如果我理解每个人,这应该可以解决我的问题。
猜你喜欢
  • 1970-01-01
  • 2013-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多