java - 图像图标未显示在 JButton 上

标签 java user-interface jbutton packaging imageicon

我目前正在用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/

相关文章:

java - 如何将 Char 数组的子字符串传递给 Java 的另一个函数?

java - 在 Java 中执行符号数学

java - 如何使 JFrame 窗口仅在我按下前一个 JFrame 窗口的 JButton 后才出现?

java - 对一组 jButton 实现 ActionListener 时出现问题

java - 从 JButton 中删除三个点 "..."?

java - 当我为已经实例化的类调用 Class.getInstance 时会发生什么?

java - 为什么 FileInputStream 中的 read() 返回一个整数?

java - 有没有办法让Jikes RVM通过GUI运行Java应用程序?

user-interface - 使用 Pascal 构建 GUI 表单

java - 涉及 actionListener 的类不是抽象的