【问题标题】:making a chessboard with arrays of JPanel用 JPanel 数组制作棋盘
【发布时间】:2015-05-08 19:52:10
【问题描述】:

我正在做一个棋盘,我的想法是制作一个 JPanel 数组,每个盒子都是一个带有颜色的 JPanel,我遇到的问题是,当我进行这样的分配时 "chessboard [ rows ] [columns ] = b" 和 compile 给了我一个例外。

为什么作业给我一个错误?

我该如何解决?

是布局网格,应该放在JFrame还是JPanel中?

谢谢。

线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常:0

头等舱

public class ChessBoard extends JPanel {
    private JPanel[][] chessBoardSquares = new JPanel[8][8];

    private final JPanel tile;


    public ChessBoard () {
        tile = new JPanel();
         tile.setLayout(new GridLayout(8, 8));

        for (int rows = 0; rows < 8; rows++) {
            for (int columns = 0;columns< 8;columns++) {
               JPanel b = new JPanel();

                 chessBoardSquares  = new JPanel[rows][columns];

                if ((rows+columns +1)%2 == 0){
                    b.setBackground(Color.WHITE);
               chessBoardSquares[rows][columns] = b;
                }

               tile.add(   chessBoardSquares [rows][columns]);
            }


        }

    }

}

主要

public class example {
    public static void main(String[] args) {



JFrame window = new JFrame();

 ChessBoard chessBoard = new  ChessBoard();


    window.add( chessBoard  );
    window.setVisible(true);



    }

}

【问题讨论】:

  • 您多久初始化一次chessBoardSquares?我认为如果你在内部 for 循环中执行它有点太多了。 `
  • 老师告诉我应该在循环中保存内存。我已经看到错误并解决了,非常感谢。

标签: java arrays swing


【解决方案1】:

你没有正确填充你的数组。

这个

    for (int rows = 0; rows < 8; rows++) {
        for (int columns = 0;columns< 8;columns++) {
           JPanel b = new JPanel();

             // ***** this creates a completely new array *******
             chessBoardSquares  = new JPanel[rows][columns];

应该是

    for (int rows = 0; rows < 8; rows++) {
        for (int columns = 0;columns< 8;columns++) {
           JPanel b = new JPanel();

             // this assigns a JPanel to an array item
             chessBoardSquares[rows][columns] = new JPanel();

另外顺便说一句,我看不到您将平铺 JPanel 添加到 GUI 的位置,您需要解决此问题。

【讨论】:

    【解决方案2】:

    这样应该更好:

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class ChessBoard extends JPanel {
    
        private final JPanel[][] tiles = new JPanel[8][8];
    
        public ChessBoard() {
            Dimension dims = new Dimension(64, 64);
            setLayout(new GridLayout(8, 8));
            for (int i = 0; i < 8; i++) {
                for (int j = 0; j < 8; j++) {
                    JPanel b = new JPanel();
                    b.setPreferredSize(dims);
                    b.setMinimumSize(dims);
                    if ((i + j + 1) % 2 == 0) {
                        b.setBackground(Color.WHITE);
                    } else {
                        b.setBackground(Color.BLACK);
                    }
                    add(b);
                    tiles[i][j] = b;
                }
            }
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame("Chess");
            ChessBoard chessBoard = new ChessBoard();
            f.add(chessBoard);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-31
      • 1970-01-01
      • 1970-01-01
      • 2013-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多