用JButtons制作棋盘 [英] Making a Chess Board out of JButtons

查看:50
本文介绍了用JButtons制作棋盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为项目制作棋盘,因此必须使用JButtons.我正在尝试仅在每个图块上设置一个空白图像的木板,这就是我的代码:

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:

驱动程序

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

国际象棋广场

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;
  }
}

国际象棋棋盘

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:

线程"main"中的异常java.lang.NullPointerException在 ChessBoard(ChessBoard.java:23)位于Driver.main(Driver.java:8)

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 :)

推荐答案

可能的原因之一是ImageIcon empty = new ImageIcon("/pieces/EmptySquare.jpg"); ...

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

图像的路径表明您正在使用嵌入式资源,但是ImageIcon(String)会将值当作文件来对待,您不能使用嵌入式资源来执行此操作,因为它们不是文件.

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.

相反,您需要使用类似ImageIcon empty = new ImageIcon(getClass().getResource("/pieces/EmptySquare.jpg"));的东西.

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

就个人而言,我建议您使用ImageIO.read(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);也将不起作用,因为您尚未为grid[x][y]

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...

已更新...

您应该使用board.add(grid[x][y]);而不是board.setContentPane(grid[x][y]);,否则,您将用按钮替换内容窗格...由于只能有一个内容窗格,因此您只会获得一个按钮...

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...

这篇关于用JButtons制作棋盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆