Java 如何阻止禁用时JButton变为灰色?

Java 如何阻止禁用时JButton变为灰色?,java,swing,user-interface,jbutton,disabled-control,Java,Swing,User Interface,Jbutton,Disabled Control,我得写一个纸牌游戏。单击某张卡时,会生成随机卡图像,但由于您只能单击该卡一次,因此该按钮在单击后设置为禁用。如何防止卡图像在单击后变为灰色,以便新生成的卡图像清晰可见 //Actions performed when an event occurs public void actionPerformed(ActionEvent e) { if (e.getSource() == card1) { randomInteger(); card1.setIcon(ca

我得写一个纸牌游戏。单击某张卡时,会生成随机卡图像,但由于您只能单击该卡一次,因此该按钮在单击后设置为禁用。如何防止卡图像在单击后变为灰色,以便新生成的卡图像清晰可见

//Actions performed when an event occurs
public void actionPerformed(ActionEvent e)
{

    if (e.getSource() == card1)
    {
    randomInteger();
    card1.setIcon(cardImages[randomInt]);
    card1.setEnabled(false);

    }
    else if (e.getSource() == card2)
    {
    randomInteger();
    card2.setIcon(cardImages[randomInt]);
    card2.setEnabled(false);
    }
    else if (e.getSource() == card3)
    {
    randomInteger();
    card3.setIcon(cardImages[randomInt]);
    card3.setEnabled(false);
    }
    else if (e.getSource() == card4)
    {
    randomInteger();
    card4.setIcon(cardImages[randomInt]);
    card4.setEnabled(false);
    }
    else
    {
    randomInteger();
    card5.setIcon(cardImages[randomInt]);
    card5.setEnabled(false);
    }

}

}

您只需将按钮的禁用图标设置为与按钮图标相同的值。请参见此示例:

在左边有一个按钮,我在这里设置了图标和禁用图标。在右侧,我只设置了图标:

import java.awt.BorderLayout;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class TestDisabledButtons {

    public static final String CARD_URL = "http://assets0.wordansassets.com/wvc-1345850020/wordansfiles/images/2012/8/24/156256/156256_340.jpg";

    protected void createAndShowGUI() throws MalformedURLException {
        JFrame frame = new JFrame("Test button");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ImageIcon imageIcon = new ImageIcon(new URL(CARD_URL));
        JButton button = new JButton(imageIcon);
        JButton button2 = new JButton(imageIcon);
        button.setDisabledIcon(imageIcon);
        button.setEnabled(false);
        button2.setEnabled(false);
        frame.add(button, BorderLayout.WEST);
        frame.add(button2, BorderLayout.EAST);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    new TestDisabledButtons().createAndShowGUI();
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }

}


也许我不太清楚你想做什么,但我认为最好使用JToggleButton来完成你的任务我们必须使用一个简单的JButton,它会有一个预先设置好的卡片图像,然后当你点击卡片时,一种方法会生成一张随机的卡片,比如king,queen,但一旦卡被点击,按钮将被禁用,因此生成的图像将褪色。我怎样才能阻止这种事发生?我想你不能这么做。因此,我建议使用切换按钮并编写监听器来检查按钮位置。显然,我可以删除操作监听器,然后在用户单击重置时重新应用它,但它似乎对我不起作用。我认为从按钮
加1删除操作监听器肯定不是一个好主意;但是有人知道如何防止
JButton
上的标准文本“变灰”吗