禁用按钮时如何阻止文本变灰?

时间:2016-04-15 16:33:01

标签: java swing

我目前的java版本是:

$java -version
 java version 1.8.0_40

我是一个非常新的摇摆和尝试编写我的第一个扫雷应用程序。现在,我需要在点击它时禁用按钮并打印多个相邻的地雷(具有相应的文字颜色取决于数字:1-蓝色,2 - 绿色,3 - 红色等)。正如this answer中所建议的那样,我写了以下内容:

JFrame frame = new JFrame("Hello swing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JButton button = new JButton();
button.addActionListener((ActionEvent ev) -> { 
                        button.setSelected(true);  
                        button.setText("<html><font color = red>3</font></html>");
                        button.setEnabled(false); });

但禁用时仍然会变灰:

enter image description here

但启用按钮看起来就像我想要的那样:

enter image description here

如何解决这个问题?如何通过禁用按钮解决此问题?

1 个答案:

答案 0 :(得分:2)

  

也许你知道一些解决方法

使用图标而不是文字。

然后你可以使用:

JButton button = new JButton( icon3 );
button.setDisabledIcon( icon3 );
相关问题