如何使 java 中的 JButton 不可点击?

how to make a JButton not clickable in java?

我正在寻找 .setEnabled(false) 的替代品,这样按钮就无法点击了。

我听说我可以从按钮中删除 ActionListener,但我只想让特定按钮不可点击,但我不知道该怎么做。

或者我怎样才能去掉点击按钮后的 "selected" 效果?

我认为问题是按钮看起来是灰色的,因为 setEnabled 确实是您禁用按钮的方式 - 这只是意味着使其不可点击(或者您是否希望它响应键盘输入?)。如果是这种情况,那么您可以使用 html:

更改它的外观
button.setText("<html><font color=black>3</font></html>");

Because in my application I have a grid of JButtons that are used as icons

然后不要使用按钮,使用也支持图标的 JLabel。

或者使用也可以支持网格的 JList。

或者,如果您使用 JButton,那么您需要如下代码:

JButton button = new JButton(...);
button.setBorderPainted( false );
button.setFocusPainted( false );