我目前正在用java制作一个游戏,并且有一个带有图像图标的JButton。唯一的问题是图像没有显示,甚至调试窗口中也没有抛出错误。
我已经打包了我的程序(参见屏幕截图 - https://db.tt/N9CwHJdf)。我使用的代码写在下面,如果有人能解决这个问题,我将非常感激。谢谢。
//Button Image
ImageIcon diceIcon = new ImageIcon("Client/images/DiceIcon.png");
//Create Button
JButton rollDice = new JButton("Roll Dice", diceIcon);
rollDice.setForeground(Color.darkGray);
rollDice.setFocusPainted(false);
rollDice.setPreferredSize(new Dimension(284,50));
rollDice.setBorder(BorderFactory.createLineBorder(Color.orange));
rollDice.setBackground(Color.orange);
rollDice.setToolTipText("Click to roll dice and continue playing");
rollDice.addActionListener(this);
最佳答案
您可以像这样加载 ImageIcon:
ImageIcon diceIcon = new ImageIcon(getClass().getResource("/images/DiceIcon.png"));
阅读 How to Use Icons 上的 Java 教程了解更多。
关于java - 图像图标未显示在 JButton 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20529406/