0

我正在学习Java并尝试制作国际象棋游戏。我要做的第一件事是将框架设置为板,然后使用 8x8 的 JButton 网格在板上制作正方形,但似乎我的一些代码根本没有被使用。

我的框架将加载,但没有添加任何按钮,我用来存储每个正方形位置的数组没有被使用,显然我想要添加按钮的网格也没有。我几乎修改了代码的每个部分,但无法弄清楚。

我已经添加了 Eclipse 在引号中给出警告的地方 //"like this"

这是我的棋盘类:

import java.awt.GridLayout;
import java.io.*;
import javax.swing.ImageIcon; //"the import is never used"
import javax.swing.JFrame;
import javax.swing.JButton; //"the import is never used"
import Logic.ChessSquare;



public class ChessBoard {

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

        //create grid and grid dimensions
        GridLayout grid = new GridLayout(8,8); //"the value of the local variable grid is never used"

        //create frame and set specifications of frame
        JFrame frame = new JFrame();
        frame.setVisible(true);
        frame.pack();
        frame.setTitle("I should have started this sooner");
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        //initialise 3D array
        int[][] square; //"the value of the local variable square is never used"

        //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++){
                ChessSquare chessSquare = new ChessSquare(i, l);
                square = new int[i][l];
                frame.add(chessSquare);
            }
        }



    }

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

}

这是我的 ChessSquare 课:

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
@SuppressWarnings("serial")
public class ChessSquare extends JButton {


    //instance variables for position and piece
    public int posX;
    public int posY;
    public String selectedPiece;

    //constructor for chess squares, loads image and adds to new JButton
    public ChessSquare(int x, int y) throws IOException {
                BufferedImage buttonIcon = ImageIO.read(new File("E:\\Eclipse\\ChessF\\src\\Images\\EmptySquare.jpg"));
                JButton button = new JButton(new ImageIcon(buttonIcon)); //"the value of the local variable button is never used"
                setVisible(true);
                }

    //accessor method for position
    public void squarePos(int x, int y){
        this.posX = x;
        this.posY = y;
    }

}

先感谢您。

PS 当我改变 frame.add(chessSquare); 到 frame.add(ChessSquare); (如下所示)警告消失,但随后出现错误“ChessSquare 无法解析为变量”

//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++){
        ChessSquare chessSquare = new ChessSquare(i, l);
        square = new int[i][l];
        frame.add(ChessSquare);
    }
}

Ryan,这是更新后的代码,我将上面的旧代码留作比较:

国际象棋广场:

package Logic;

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

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

//chess square class, 1 instance of which for each square in the grid
@SuppressWarnings("serial")
public class ChessSquare extends JButton {


    //instance variables for position and piece
    public int posX;
    public int posY;
    public String selectedPiece;

    //constructor for chess squares, loads image and adds to new JButton
    public ChessSquare(int x, int y) throws IOException {
                BufferedImage buttonIcon = ImageIO.read(new File("E:\\Eclipse\\ChessF\\src\\Images\\EmptySquare.jpg"));
                this.setIcon((Icon) buttonIcon);
                setVisible(false);
                }

    //accessor method for position
    public void squarePos(int x, int y){
        this.posX = x;
        this.posY = y;
    }

}

棋盘:

import java.awt.GridLayout;
import java.io.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JButton;
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.setLayout(grid);
        frame.pack();
        frame.setTitle("I should have started this sooner");
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);


        //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++){
                ChessSquare chessSquare = new ChessSquare(i, l);
                int[][] square = new int[i][l];
                frame.add(chessSquare);
            }
        }



    }

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

}
4

2 回答 2

2

好的,从顶部开始,您的“从不使用导入”,意味着在该文件中,您不使用这些导入,因此可以安全删除它们,除非您打算在该文件中使用它们,这通常在您使用 eclipse 时发生导入该项目,但后来决定不使用它。(即删除它)

其次,正如我在编辑之前所说,您的网格布局未添加到您的 jframe

第三,您的正方形说“从未使用过该值”,但这是eclipse的错误,实际上您实际上是在使用它,这只是说因为它是在for循环内部初始化的。

chesssquare在你的最后一个框中,添加而不是类名是正确的ChessSquare

这可能是我目前能做的最好的事情了。玩得开心!

编辑:

您也JButton button遇到同样的问题,您没有在其他任何地方使用它:)

于 2013-03-22T06:23:49.677 回答
1

自从我写 Swing 以来已经有一段时间了,但我会试一试......

GridLayout grid = new GridLayout(8,8); //"the value of the local variable grid is never used"

构建GridLayout是不够的,你还必须使用它。例如frame.setLayout(grid);

int[][] square; //"the value of the local variable square is never used"

真的不是这样。您可能应该删除该行和初始化它 64 次的错误行。

JButton button = new JButton(new ImageIcon(buttonIcon)); //"the value of the local variable button is never used"

ChessSquare是一个JButton。您无需在此处创建另一个。相反,您应该调用类似this.setIcon(new ImageIcon(buttonIcon));

同样,我的 Swing 有点生疏了,但我猜这些答案很接近。

于 2013-03-22T06:47:01.977 回答