我正在为一个项目制作国际象棋游戏,我要做的第一件事就是创建一个框架,然后用64个JButton(有组织的8x8)填充它,每个JButton都将充当棋盘上的正方形。我对Java还是很陌生,但是我仍然不认为我应该得到我得到的错误,它不是一会儿发生的,但是那表示当框架确实加载之前,没有一个JButtons做到了。

在使用3D数组向我的JButton添加坐标时,我似乎遇到了问题,我不断收到错误消息“对于ChessBoard类型,未定义add(ChessSquare)方法”,最重要的是Eclipse不断为我提供帮助犯了我的错误,但我认为接受这些错误可能会使事情变得更糟。

另一个问题是当我尝试从ChessSquare的3D数组中保存正方形的坐标时。

我目前有2类,ChessBoard和ChessSquare,我想让ChessBoard使用ChessSquare来制作作品。

抱歉,如果我不太清楚,我会非常累。

在此先感谢您的帮助。

这是我的代码:

板:

import java.awt.GridLayout;
import java.io.IOException;
import javax.swing.JFrame;
import Logic.ChessSquare;


public class ChessBoard {

    //chess board constructor
    public ChessBoard() throws IOException {

        //create grid and grid dimensions
        GridLayout grid = new GridLayout(8,8);

        //create frame and set specifications of frame
        JFrame frame = new JFrame();
        frame.setVisible(true);
        frame.setSize(458, 458);
        frame.setTitle("I'm starting to prefer C");

        //initialise 3D array
        ChessSquare[][] square = new ChessSquare[8][8];

        //create 64 instances of ChessSquare and assign each square as an element in the 3D array
        for (int i = 0; i < 8; i++){
            for(int l = 0; l < 8; l++){
                square[i][l] = new ChessSquare();
                this.squarePos(square[i][l]); //this is where my main gripe is
            }
        }



    }

    public static void main(String[] args) throws IOException{
        new ChessBoard();
    }

}


广场:

package Logic;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;

//chess square class, 1 instance of which for each square in the grid
public class ChessSquare extends JButton {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    //instance variables for position and piece
    public int squarePos;
    public String selectedPiece;

    //accessor method for position
    public void squarePos(){
        int squarePos = ChessBoard.square;
    }


    //constructor for chess squares
    public ChessSquare() throws IOException {
                BufferedImage buttonIcon = ImageIO.read(new File("E:\\Eclipse\\ChessF\\src\\Images\\EmptySquare.jpg"));
                JButton button = new JButton(new ImageIcon(buttonIcon));
                }
}

最佳答案

“另一个问题是,当我尝试从ChessSquare的3D数组中保存正方形的坐标时”

xy作为ChessSquare的属性,并在ChessSquare的构造函数中接收值:

public class ChessSquare extends JButton {
    private int x, y;

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    //instance variables for position and piece
    public int squarePos;
    public String selectedPiece;

    public ChessSquare(int x, int y){
        this.x = x;
        this.y = y;
    }
...


为了初始化正方形并渲染它们,请在ChessBoard的构造函数中修改循环。您需要将新创建的ChessSquare添加到框架中。

    for (int i = 0; i < 8; i++){
        for(int l = 0; l < 8; l++){
            ChessSquare square = new ChessSquare(i, l);
            square[i][l] = square;
            frame.add(square);
        }
    }


之后,每个ChessSquare都知道其xy位置。

Oracle有一些很棒的教程:http://docs.oracle.com/javase/tutorial/java/TOC.html

同样,在掌握了基础知识之后,请继续阅读Swing:http://docs.oracle.com/javase/tutorial/uiswing/components/index.html

09-27 03:39