“不使用局部变量的值”的多个实例

时间:2013-03-22 06:17:22

标签: java eclipse swing jbutton chess

我正在学习Java并尝试制作国际象棋游戏。我想要做的第一件事就是将一个框架设置为一块板,然后使用一个8x8的JButtons网格在板上制作正方形,但是看起来我的部分代码根本就没用了。

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

我已经添加了Eclipse在引号中发出警告的地方     //“喜欢这个”

这是我的ChessBoard课程:

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

}

提前谢谢。

P.S。当我改变frame.add(chessSquare); to 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,这是更新后的代码,我将上面的旧代码留下来进行比较:

ChessSquare:

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

}

器ChessBoard:

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

}

2 个答案:

答案 0 :(得分:2)

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

第二,正如我在编辑之前所说,你的gridlayout没有添加到你的jframe

第三,你的方块说“值永远不会被使用”但这是eclipse的一个错误,你实际上实际上正在使用它,它只是说因为它是在for循环中初始化的。

在您的上一个方框中,添加chesssquare而不是类名ChessSquare

是正确的

这可能是我现在能做的最好的事情。玩得开心!

编辑:

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

答案 1 :(得分: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"

ChessSquareJButton。你不需要在这里创建另一个。相反,你应该调用像this.setIcon(new ImageIcon(buttonIcon));

这样的东西

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