如何使 JButton 在 Java 中不可点击?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29054963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to make a JButton not clickable in java?
提问by redCodeAlert
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?
或者如何在单击按钮后摆脱“选定”效果?
回答by camickr
Because in my application I have a grid of JButtons that are used as icons
因为在我的应用程序中,我有一个用作图标的 JButton 网格
Then don't use buttons, use a JLabel which also supports an 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 );
回答by mk.
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?). If that's the case, then you can change the way it looks by using html:
我认为问题在于按钮看起来如何变灰,因为setEnabled
实际上是禁用按钮的方式 - 这只是意味着使其不可点击(或者您是否希望它响应键盘输入?)。如果是这种情况,那么您可以使用 html 更改它的外观:
button.setText("<html><font color=black>3</font></html>");