有没有办法自定义Java的setEnabled(false)?
假设您有一个 JCheckBox
想要用作开/关指示器。您可以使用 setEnabled(false)
禁用 JCheckBox
上的鼠标单击。但是 setEnabled(false)
也会使复选框和复选框的文本变灰。有没有办法自定义 setEnabled(false)
以使灰显不会发生?
如果这是不可能的,唯一的解决方案是自定义 ButtonModel
吗?
Let's say you have a JCheckBox
you want to use as an on/off indicator. You use setEnabled(false)
to disable mouse clicks on the JCheckBox
. But setEnabled(false)
also grays out the checkBox and the checkBox's text. Is there a way to customise setEnabled(false)
so that the graying out does not happen?
If that's not possible, is the only solution something along the lines of customising a ButtonModel
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以子类化 JCheckBox 并重写 processMouseEvent/processKeyEvent 以不执行任何操作。
You can subclass JCheckBox and override processMouseEvent/processKeyEvent to not do anything.
扩展
JCheckBox
可以让您重写setEnabled
方法,并将您的CustomJCheckBox
传递给任何需要JCheckBox
的代码:一个参数。这应该使您不必重新实现复选框中的其他所有内容,就像实现 ButtonModel 一样。我对您使用该复选框的用途有点困惑——它未启用,但您仍然希望根据其他一些参数来显示或不显示检查?在这种情况下,为什么要使用输入元素呢?
Extending the
JCheckBox
would let you override thesetEnabled
method, and pass yourCustomJCheckBox
to any code expecting aJCheckBox
as a parameter. That should keep you from having to reimplement everything else from the check box, as you would for implementing a ButtonModel.I am a little confused at what you're using the check box for -- it's not enabled, but you still want a check to appear or not, based on some other parameter? Why use an input element, in that case?
已请求更改禁用的 JCheckbox 中的文本颜色,这在 Sun Bug 数据库中仍然是一个未解决的问题:
[6289684] 更改禁用的 JCheckbox 文本颜色的方法
查看错误详细信息以获取解决方法。
Changing the colour of text in a disabled JCheckbox has been requested and is still an open issue in the Sun Bug Database:
[6289684] Way to change the color of disabled JCheckbox text
Take a look at the bug details for a workaround.
您可以自定义 Button UI 来执行此操作。
You customize Button UI to do this.