我正在学习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();
}
}