0

我正在和我的朋友一起做一个跳棋游戏,我负责棋子、棋盘等。我通过另一个也在玩跳棋游戏并让它工作的朋友得到了使用 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);
    }
}
4

2 回答 2

1

用于导入图像使用 BufferedImage

BufferedImage buttonIcon = ImageIO.read(new File("buttonIconPath"));

并用于创建按钮

button = new JButton(new ImageIcon(buttonIcon));

之后用于button.setContentAreaFilled(false);显示图像,仅此而已

于 2013-01-15T22:43:04.913 回答
1

您需要检查您的应用程序期望您的图像驻留的位置。添加

System.out.println(System.getProperty("user.dir"));

并将您的图像复制到该位置。

于 2013-01-15T22:46:08.763 回答