简体   繁体   English

Java-禁用的JButton上的图标/图像为灰色

[英]Java - Icon/image is grey on a disabled JButton

I'm making minesweeper for a school project. 我正在为学校项目做扫雷车。 When the player wins or loses, the mines are revealed. 当玩家获胜或失败时,地雷就会显示出来。 Their buttons are disabled, and icons of flags/mines will appear. 他们的按钮被禁用,并且会出现标志/地雷的图标。 The problem is that the icons turn grey when the buttons are disabled. 问题是禁用按钮时图标变为灰色。 Is there a way around this? 有没有解决的办法?

I have also tried setting the text of the JButton to something like "<html><img src=\\"res\\\\mine.png\\"/></html>" but it showed some weird image. 我还尝试将JButton的文本设置为"<html><img src=\\"res\\\\mine.png\\"/></html>"但显示了一些奇怪的图像。

Update: I tried using setDisabledIcon() but nothing's showing up. 更新:我尝试使用setDisabledIcon()但没有任何显示。 Here's some pseudo-code 这是一些伪代码

The buttons I use for the minefield is a class called Field , which extends JButton 我用于雷区的按钮是一个名为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);
            }
        }
    }
}

Figured this out in a test 在测试中想通了

For some reason 由于某些原因

clickedButton.setDisabledIcon(mineIcon)

Alone doesn't do anything. 一个人什么也没做。

But: 但:

clickedButton.setIcon(mineIcon)
clickedButton.setDisabledIcon(mineIcon)

Will show whatever icon I wanted 将显示我想要的任何图标

a JButton actually allows seven associated images: the main image (use setIcon to specify it if not supplied in the constructor), the image to use when the button is pressed (setPressedIcon), the image to use when the mouse is over it (setRolloverIcon, but you need to call setRolloverEnabled(true) first), the image to use when the button is selected and enabled (setSelectedIcon), the image to use when the button is disabled (setDisabledIcon) , the image to use when it is selected but disabled (setDisabledSelectedIcon), and the image to use when the mouse is over it while it is selected (setRolloverSelectedIcon). 一个JButton实际上允许七个相关的图像:主图像(如果未在构造函数中提供,则使用setIcon进行指定),按下按钮时使用的图像(setPressedIcon),鼠标悬停时使用的图像(setRolloverIcon ,但是您需要先调用setRolloverEnabled(true),选择并启用按钮时要使用的图像(setSelectedIcon), 禁用按钮时要使用的图像(setDisabledIcon) ,选择按钮时要使用的图像,但是禁用(setDisabledSelectedIcon),以及在选中鼠标时将鼠标悬停在其上使用的图像(setRolloverSelectedIcon)。 - http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JButton.html -http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JButton.html

so use setDisabledIcon(ImageIcon) 所以使用setDisabledIcon(ImageIcon)

The greyed image is the automatically generated one, in case you want a different icon, use setDisabledIcon() 灰色图像是自动生成的图像,如果您需要其他图标,请使用setDisabledIcon()

Icon disabledIcon = new ImageIcon("youricon.gif");
button.setDisabledIcon(disabledIcon);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM