Making a Chess Board out of JButtons

2019-08-08 21:21发布

问题:

I'm making a chessboard for a project and I have to use JButtons. I'm trying to just set the board up with a blank image I have for each tile, this is the code I have:

Driver

public class Driver
{
  public static void main (String[] args)
  {
    new ChessBoard();
  }
}

ChessSquare

import javax.swing.*;
import java.awt.*;

public class ChessSquare 
{
  public ImageIcon pieceImage;
  /** The square's location */
  private int xCoord;
  private int yCoord;

  /** Constructor for the squares */
  public ChessSquare(ImageIcon thePieceImage, int theXCoord, int theYCoord)
  {
    pieceImage = thePieceImage;
    xCoord = theXCoord;
    yCoord = theYCoord;
  }

  public int getXCoord()
  {
    return xCoord;
  }

  public int getYCoord()
  {
    return yCoord;
  }
}

ChessBoard

public class ChessBoard 
{
  public ChessBoard()
  {
  JFrame board = new JFrame();
  board.setTitle("Chess Board!");
  board.setSize(500,500);
  board.setLayout(new GridLayout(8,8));

  JPanel grid[][] = new JPanel[8][8];

  ImageIcon empty = new ImageIcon("/pieces/EmptySquare.jpg");

  for(int x = 0; x<8; x++)
  {
    for(int y = 0; y<8; y++)
    {
      ChessSquare s = new ChessSquare(empty, x, y);
      JButton square = new JButton(s.pieceImage);
      grid[x][y].add(square);
      board.setContentPane(grid[x][y]);
    }
  }
  board.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  board.setVisible(true);
  }
}

My code compiles fine but when I run it I get this error:

Exception in thread "main" java.lang.NullPointerException at ChessBoard.(ChessBoard.java:23) at Driver.main(Driver.java:8)

I don't know what to do to fix this error. Thanks for any help :)

回答1:

One of the likely causes is ImageIcon empty = new ImageIcon("/pieces/EmptySquare.jpg");...

The path of the image suggest that you are using embedded resources, but ImageIcon(String) treats the value as if it were a file, you can't do this with embedded resources, they aren't files.

Instead, you need to use something more like ImageIcon empty = new ImageIcon(getClass().getResource("/pieces/EmptySquare.jpg"));.

Personally, I'd recommend that you should be using ImageIO.read(getClass().getResource("/pieces/EmptySquare.jpg")) as this will throw an exception if the resource can not be loaded from some reason, rather then failing silently.

grid[x][y].add(square); also won't work, as you've not assigned anything to grid[x][y]

grid[x][y] = new JPanel();
grid[x][y].add(square);

Might work better, but I don't know why you're doing this, when doing something like...

JButton grid[][] = new JButton[8][8];

//...

grid[x][y] = square;

Would seem to be more logical for what you are trying to achieve...

Updated...

Instead of board.setContentPane(grid[x][y]); you should be using board.add(grid[x][y]);, other wise you will replace the content pane with the button...since there can only be a single content pane, you'll only get one button...



回答2:

In grid[x][y].add(square); you are actually calling JPanel.add(), because each element in the array is a JPanel.

So the error is because grid[x][y] is still null you will get a NullPointerException if you call a method on it, like add() in this case.

You want to assign the value since you are using an array

grid[x][y] = square;