简体   繁体   English

如何让JButton在java中无法点击?

[英]how to make a JButton not clickable in java?

I'm looking for a substitute of .setEnabled(false) , so that the button is not clickable. 我正在寻找.setEnabled(false)的替代品,因此按钮不可点击。

I have read that I could remove the ActionListener from the button, but I just want the specific buttons to be not clickable, and do not know how to do it anyway. 我已经读过我可以从按钮中删除ActionListener,但我只是希望特定的按钮不可点击,并且无论如何都不知道如何操作。

Or how can I get rid of the "selected" effect after clicking a button? 或者如何在单击按钮后摆脱“选定”效果?

Because in my application I have a grid of JButtons that are used as icons 因为在我的应用程序中,我有一个用作图标的JButtons网格

Then don't use buttons, use a JLabel which also supports an Icon. 然后不要使用按钮,使用也支持Icon的JLabel。

Or maybe use a JList which can also support a grid. 或者也许使用也可以支持网格的JList。

Or if you use a JButton then you need code like: 或者,如果您使用JButton,那么您需要以下代码:

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

I assume that the problem is how the button looks grayed out, because setEnabled really is the way you disable a button - which just means making it not clickable (or did you want it to respond to keyboard input?). 我假设问题是按钮看起来是灰色的,因为setEnabled实际上是禁用按钮的方式 - 这只是意味着它不能被点击(或者你想让它响应键盘输入?)。 If that's the case, then you can change the way it looks by using html: 如果是这种情况,那么您可以使用html更改其外观:

button.setText("<html><font color=black>3</font></html>");

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

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