我目前正在用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教程以获取更多信息。

07-24 09:25