gpt4 book ai didi

java - IconImage 未显示在 jButton 上

转载 作者:行者123 更新时间:2023-12-02 07:20:18 24 4
gpt4 key购买 nike

我正在和我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com