我目前正在用java製作遊戲並且有一個JButton,它有一個圖像圖標。唯一的問題是沒有顯示圖像,甚至在調試窗口中也沒有發生錯誤。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);
DiceIcon = new ImageIcon(「Client/images/DiceIcon.png」);嘗試給出完整的圖像路徑 – user2277872
我已經添加了以下行:DiceIcon = new ImageIcon(「src/Client/images/DiceIcon.png」);並仍然不起作用:( –
嘗試從您的項目中的開始文件獲取路徑,並查看是否有效...圖像是棘手的當涉及到路徑 – user2277872