我和我的朋友正在制作一个跳棋游戏,我负责这些棋子,棋盘等等。我想到了通过另一位正在制作跳棋游戏并让它正常工作的朋友使用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);
}
}
答案 0 :(得分:1)
用于导入图像使用BufferedImage
BufferedImage buttonIcon = ImageIO.read(new File("buttonIconPath"));
并创建按钮
button = new JButton(new ImageIcon(buttonIcon));
然后使用button.setContentAreaFilled(false);
来显示图像,而不是其他
答案 1 :(得分:1)
您需要检查应用程序预期图像的驻留位置。添加
System.out.println(System.getProperty("user.dir"));
并将图像复制到该位置。