java - IconImage 未显示在 jButton 上

标签 java swing jbutton imageicon

我正在和我的 friend 一起制作西洋跳棋游戏,我负责棋子、棋盘等。我通过另一个也在开发西洋跳棋游戏的 friend 得到了使用 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);
    }
}

最佳答案

导入图像使用BufferedImage

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

并用于创建按钮使用

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

然后使用 button.setContentAreaFilled(false); 显示图像而不显示其他内容

关于java - IconImage 未显示在 jButton 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14348081/

相关文章:

java - 当收到 NumberFormatException 时,如何阻止或重做 SWING 输入?

java - Swing:在 JPanel 中绘制图形会改变其尺寸吗?

java repaint 屏蔽了 jbutton

java - 向 JScrollpane 添加按钮

java - 如何从 Antlr4 语法在控制台上打印一些内容

java - 读取 IDOC 时如何控制事务(提交/回滚)

java JOGL 和类似资源

java - 组件中的多语言

java - 在 android 中接收彩信并将其显示在我的应用程序中作为 ListView

java - 如何删除 JButton 矩阵中的 JButton?