Ale*_*lex 1 java icons swing image
我正在为一个学校项目做扫雷.当玩家获胜或失败时,地雷将被揭露.他们的按钮被禁用,标志/地雷的图标将出现.问题是禁用按钮时图标变为灰色.有没有解决的办法?
我也试过设置JButton类似的文本,"<html><img src=\"res\\mine.png\"/></html>"但它显示了一些奇怪的图像.
更新:我试过使用setDisabledIcon()但没有出现.这是一些伪代码
我用于雷区的按钮是一个叫做的类Field,它扩展了JButton
mouseReleased(mouseEvent e) {
Field fieldClicked = (Field)e.getSource();
if fieldClicked is mine {
fieldClicked.setEnabled(false);
gameTimer.stop();
setLost(true);
loop through 2D array of fields {
if field is a mine {
field.setDisabledIcon(Field.mineIcon);// public static final icon of Field. mineIcon = new ImageIcon("res\\mine.png")
field.setEnabled(false);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在测试中想出这个
由于某些原因
clickedButton.setDisabledIcon(mineIcon)
Run Code Online (Sandbox Code Playgroud)
一个人什么都不做.
但:
clickedButton.setIcon(mineIcon)
clickedButton.setDisabledIcon(mineIcon)
Run Code Online (Sandbox Code Playgroud)
将显示我想要的任何图标