私は Java を学んでおり、チェス ゲームを作ろうとしています。私が最初にやりたいことは、フレームをボードとして設定し、JButton の 8x8 グリッドを使用してボード上に正方形を作成することですが、コードの一部がまったく使用されていないようです。
私のフレームは読み込まれますが、ボタンは追加されません。各正方形の位置を格納するために使用している配列は使用されておらず、ボタンを追加したいグリッドも明らかに使用されていません。コードのほぼすべての部分をいじりましたが、理解できません。
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;
    }
}
前もって感謝します。
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);
    }
}
ライアン、これが更新されたコードです。比較のために古いものを残しました。
チェススクエア:
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();
    }
}