我正在和我的朋友一起做一个跳棋游戏,我负责棋子、棋盘等。我通过另一个也在玩跳棋游戏并让它工作的朋友得到了使用 iconImages 的想法。由于某种原因,检查器图像未显示。任何想法将不胜感激。
import java.awt.*;
import javax.swing.*;
public class checkerboard {
public static void main(String[] args) {
//Creates new checkerboard GUI
JFrame checkers = new JFrame("Checkers");
checkers.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension size = new Dimension(1050, 900);
//Creates two arrays of which black and white spaces
JButton[] blackSpaces = new JButton[32];
JButton[] whiteSpaces = new JButton[32];
//Declares and instantiates checkers
ImageIcon grayCheckers = new ImageIcon("GrayCircle.jpg");
//for (int i = 0; i < 12; i++)
//grayCheckers[i] = new ImageIcon("GrayCircle.jpg");
ImageIcon redCheckers = new ImageIcon("redcircle.jpg");
//for (int j = 0; j < 12; j++)
//redCheckers[j] = new ImageIcon("redcircle.jpg");
//instantiates black and white spaces
for (int index = 0; index < 12; index++) {
blackSpaces[index] = new JButton(grayCheckers);
blackSpaces[index].setBackground(Color.BLACK);
}
for (int index = 12; index < 20; index++) {
blackSpaces[index] = new JButton(" ");
blackSpaces[index].setBackground(Color.BLACK);
}
for (int index = 20; index < 32; index++) {
blackSpaces[index] = new JButton(redCheckers);
blackSpaces[index].setBackground(Color.BLACK);
}
for (int index = 0; index < whiteSpaces.length; index++) {
whiteSpaces[index] = new JButton(" ");
whiteSpaces[index].setBackground(Color.WHITE);
}
//Gives grid layout to panel
JPanel panel = new JPanel();
checkers.setPreferredSize(size);
checkers.setResizable(false);
panel.setLayout(new GridLayout(8, 8));
//Adds spaces in a lot of loops along with checkers to
//the first and last three lines
for (int counter1 = 0; counter1 < 4; counter1++) {
panel.add(whiteSpaces[counter1]);
panel.add(blackSpaces[counter1]);
//blackSpaces[counter1].setIcon(grayCheckers[counter1]);
}
for (int counter2 = 4; counter2 < 8; counter2++) {
panel.add(blackSpaces[counter2]);
panel.add(whiteSpaces[counter2]);
//blackSpaces[counter2].setIcon(grayCheckers[counter2]);
}
for (int counter1 = 8; counter1 < 12; counter1++) {
panel.add(whiteSpaces[counter1]);
panel.add(blackSpaces[counter1]);
//blackSpaces[counter1].setIcon(grayCheckers[counter1]);
}
for (int counter2 = 12; counter2 < 16; counter2++) {
panel.add(blackSpaces[counter2]);
panel.add(whiteSpaces[counter2]);
}
for (int counter1 = 16; counter1 < 20; counter1++) {
panel.add(whiteSpaces[counter1]);
panel.add(blackSpaces[counter1]);
}
for (int counter2 = 20; counter2 < 24; counter2++) {
panel.add(blackSpaces[counter2]);
panel.add(whiteSpaces[counter2]);
//blackSpaces[counter2].setIcon(redCheckers[(counter2 - 20)]);
}
for (int counter1 = 24; counter1 < 28; counter1++) {
panel.add(whiteSpaces[counter1]);
panel.add(blackSpaces[counter1]);
//blackSpaces[counter1].setIcon(redCheckers[(counter1 - 20)]);
}
for (int counter2 = 28; counter2 < 32; counter2++) {
panel.add(blackSpaces[counter2]);
panel.add(whiteSpaces[counter2]);
//blackSpaces[counter2].setIcon(redCheckers[(counter2 - 20)]);
}
//Displat GUI
checkers.getContentPane().add(panel);
checkers.pack();
checkers.setVisible(true);
}
}